1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define fi first #define se second #define debug(x) cerr << #x << " " << x << '\n' using namespace std; using ll = long long; using pil = pair<int,ll>; using pli = pair<ll,int>; const int N = 505; constexpr ll LINF = 0x3f3f3f3f3f3f3f3f; constexpr int mod = 1e9 + 7; int n, m, S, T, idx[N][N], tot; ll dis[N*N]; bool vis[N*N]; vector<pil> G[N*N]; inline void addedge(int u, int v, ll w) { G[u].emplace_back(v, w); G[v].emplace_back(u, w); } void Dijkstra(int s) { memset(dis, 0x3f, (tot+1)*sizeof(ll)); memset(vis, 0, (tot+1)*sizeof(bool)); dis[s] = 0; priority_queue<pli,vector<pli>,greater<pli>> q; q.push(mp(0, s)); while(!q.empty()) { int u = q.top().se; q.pop(); if(vis[u]) continue; vis[u] = 1; for(auto it : G[u]) { int v = it.fi; ll w = it.se; if(dis[v]>dis[u]+w) { dis[v] = dis[u] + w; q.push(mp(dis[v], v)); } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for(int i=1; i<n; i++) for(int j=i; j<n; j++) idx[i][j] = ++tot; S = 0; T = ++tot; auto id = [&](int x, int y) { if(x==0) return S; if(y==n) return T; return idx[x][y]; }; for(int i=1; i<n; i++) for(int j=i; j<n; j++) { addedge(id(i-1, j), id(i, j), LINF); addedge(id(i, j), id(i, j+1), LINF); } for(int i=1; i<=n; i++) addedge(id(i, i), id(i-1, i-1), LINF); for(int i=1; i<=m; i++) { int l, r, c; char dir; cin >> l >> r >> dir >> c; if(dir=='L') addedge(id(l, r-1), id(l, r), c); else addedge(id(l-1, r-1), id(l, r-1), c); } Dijkstra(S); cout << (dis[T]>=LINF ? -1 : dis[T]) << '\n'; return 0; }
|