http://acm.hdu.edu.cn/showproblem.php?pid=2544
View Code
//杭电2544 #include<stdio.h> #include<string.h> #include<stdlib.h> #define oo 1<<30 #define MAXN 1001 int map[MAXN][MAXN]; int n,m; int i,j,k; void floyd() { for(k = 1;k <= m; k++) { for(i = 1;i <= m ; i++) { for(j = 1;j <= m ; j++) { if(map[i][k]+map[k][j]<map[i][j]) { map[i][j] = map[i][k]+map[k][j]; } } } } } int main() { while(scanf("%d %d",&m,&n)!=EOF&&(m!=0||n!=0)) { for(i = 1;i <= m ; i++) { for(j = 1;j <= m ; j++) { map[i][j] = oo; } map[i][i] = 0; } int u,v,w; for(int a = 0;a <= n-1 ;a++) { scanf("%d %d %d",&u,&v,&w); if(map[u][v]>w) { map[u][v] = w; map[v][u] = w; } } floyd(); printf("%d\n", map[1][m]); } return 0; }
这个题还必须从1开始,因为题目中要求的是1与n的最短距离,所以,我很悲催的一直不对,后来才改对,看来以后还是要多多注意题中啊