标签:最短路 dijkstra算法 图论
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include<stdio.h> #include<string.h> #define mmax 101 const int INF = 1000000000; int v[mmax],d[mmax],w[mmax][mmax]; int main() { int i,j,k,n,m; int a,b,c; while(~scanf("%d%d",&n,&m),n+m) { memset(v, 0, sizeof(v)); memset(w, 0, sizeof(w)); for(i=1; i<=m; i++) { scanf("%d%d%d",&a,&b,&c); w[a][b] = c; w[b][a] = c; } for(i=1; i<=n; i++) d[i] = (i==1 ? 0 : INF); for(i=1; i<=n; i++) { int mmin = INF; for(j=1; j<=n; j++) if(!v[j] && d[j]<=mmin) mmin = d[k=j]; v[k] = 1; for(j=1; j<=n; j++) if(!v[j] && w[k][j]!=0 && d[k]+w[k][j]<d[j]) d[j] = d[k] + w[k][j]; } printf("%d\n",d[n]); } return 0; }
标签:最短路 dijkstra算法 图论
原文地址:http://blog.csdn.net/jxust_tj/article/details/27522233