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
| #include <bits/stdc++.h> #define MAX_N 400000 #define mp make_pair #define fir first #define sec second using namespace std; typedef long long lnt; typedef pair<lnt,int> pli; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, s, t, val[MAX_N+5]; vector <int> G[MAX_N+5], E[MAX_N+5]; vector <int> g[MAX_N+5], r[MAX_N+5]; struct node {int u, v, c;} a[MAX_N+5]; bool cmp (const node &p, const node &q) {return p.c < q.c;} void insert(int u, int v, int c) {G[u].push_back(v), E[u].push_back(c);} void ins(int u, int v, int c) {g[u].push_back(v), r[u].push_back(c);} lnt Dijkstra() { lnt dis[MAX_N+5]; memset(dis, 127, sizeof dis); priority_queue <pli> que; que.push(mp((dis[s] = 0), s)); while (!que.empty()) { int u = que.top().sec; lnt d = que.top().fir; que.pop(); if (dis[u] != -d) continue; for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i], c = E[u][i]; if (dis[u]+c >= dis[v]) continue; dis[v] = dis[u]+c, que.push(mp(-dis[v], v)); } } return dis[t]; } int main() { read(n), read(m), s = 1, t = m*2+2; for (int i = 1, u, v, c; i <= m; i++) read(u), read(v), read(c), val[i*2] = val[i*2+1] = c, ins(u, i*2, i*2+1), ins(v, i*2+1, i*2); for (int i = 2, tot = 0; i < n; i++, tot = 0) { for (int j = 0; j < (int)g[i].size(); j++) a[++tot] = (node){r[i][j], g[i][j], val[g[i][j]]}; sort(a+1, a+tot+1, cmp); for (int j = 1; j <= tot; j++) insert(a[j].u, a[j].v, a[j].c); for (int j = 1; j < tot; j++) insert(a[j].v, a[j+1].v, a[j+1].c-a[j].c); for (int j = 1; j < tot; j++) insert(a[j+1].v, a[j].v, 0); } for (int i = 0; i < (int)g[1].size(); i++) insert(s, g[1][i], val[g[1][i]]); for (int i = 0; i < (int)g[n].size(); i++) insert(r[n][i], t, val[g[n][i]]); return printf("%lld\n", Dijkstra()), 0; }
|