标签:
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; #define INF 1e7 + 2 int n, m; int w[102][102], d[102], v[102]; void dijkstra(int s){ memset(v, 0, sizeof(v)); for(int i=1; i<=n; i++) d[i] = i==s ? 0 : INF; for(int i=1; i<=n; i++){ int x, m = INF; for(int y=1; y<=n; y++) if(!v[y] && d[y] <= m) m = d[x = y]; v[x] = 1; for(int y=1; y<=n; y++) d[y] = min(d[y], d[x] + w[x][y]); } } int main(){ // freopen("in.txt", "r", stdin); int x, y, z; while(scanf("%d%d", &n, &m)!=EOF){ if(!n && !m) break; for(int i=1; i<=n; i++) for(int j=1; j<=i; j++) w[i][j] = w[j][i] = i==j ? 0 : INF; for(int i=0; i<m; i++){ scanf("%d%d%d", &x, &y, &z); w[x][y] = w[y][x] = min(w[x][y], z); } dijkstra(1); printf("%d\n", d[n]); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013446688/article/details/42869107