2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; const int MAXN = 1005; int dist[MAXN], len[MAXN][MAXN], vt[MAXN]; int n, m, s, t, a, b, d; void Dijkstra(int root, int finish) { memset(vt, 0, sizeof(vt)); for(int i = 1;i <= n; ++i) { dist[i] = 0x7FFFFFFF; } dist[root] = 0; vt[root] = 1; for(int i = 1;i <= n; ++i) { int minx = 0x7FFFFFFF; for(int j = 1;j <= n; ++j) { if(vt[j] == 1) continue; if(len[root][j] == 0x7FFFFFFF) continue; if(dist[j] > dist[root] + len[root][j]) { dist[j] = dist[root] + len[root][j]; } } for(int j = 1;j <= n; ++j) { if(vt[j] == 0 && dist[j] < minx) { root = j; minx = dist[j]; } } vt[root] = 1; } cout << dist[finish]<< endl; } int main() { while(scanf("%d %d", &n, &m) == 2 && n && m) { for(int i = 1;i <= n; ++i) for(int j = 1;j <= n; ++j) len[i][j] = 0x7FFFFFFF; for(int i = 1;i <= m; ++i) { scanf("%d %d %d", &a, &b, &d); if(d < len[a][b]) { len[a][b] = len[b][a] = d; } } Dijkstra(1, n); } return 0; }