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
| #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> #define ll long long #define inf 0x3f3f3f3f #define pii pair<int, int> const int mod = 1e9+7; const int maxn = 2e5+7; using namespace std; struct node {int to,w,next;} edge[maxn]; int head[maxn], cnt; int dis[maxn], vis[maxn]; int n, m, s, t; struct Spfa { void init() { memset(head,-1,sizeof(head)); memset(dis,0x3f3f3f3f,sizeof(dis)); memset(vis,0,sizeof(vis)); cnt = 0; } void add(int u,int v,int w) { edge[cnt].to = v; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt ++; } void spfa() { dis[s] = 0; vis[s] = 1; queue <int> Q; Q.push(s); while(!Q.empty()) { int now = Q.front(); Q.pop(); vis[now] = 0; for(int i = head[now]; i != -1; i = edge[i].next) { int v = edge[i].to; if(dis[v] < dis[now] + edge[i].w) { dis[v] = dis[now] + edge[i].w; if(vis[v]) continue; vis[v] = 1; Q.push(v); } } } } }sp; int main() { while(~scanf("%d%d",&n,&m) && n+m) { sp.init(); for(int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d",&u, &v, &w); sp.add(u, v, w); sp.add(v, u, w); } s = 1, t = n; sp.spfa(); printf("%d\n", dis[t]); } }
|