hdu 2544 最短路
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2544
简单最短路问题
#include<stdio.h> #define INF 1<<30 int n,m,map[105][105],dis[105]; bool bellman_ford(int start,int end) { int i,j,k; for(i = 1; i <= n; i++) dis[i] = INF; dis[start] = 0; for(k = 1; k <= n; k++) { for(i = 1; i <= n; i++) { for(j = 1 ; j <= n; j++) { if(map[i][j] < INF && dis[j] > dis[i] + map[i][j]) dis[j] = dis[i] + map[i][j]; } } } for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) if(map[i][j] < INF && dis[j] > dis[i] + map[i][j]) return false; } return true; } int main() { int i,j,a,b,c; while(~scanf("%d%d",&n,&m) && (n||m)) { for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) map[i][j] = (i == j ? 0 : INF); while(m--) { scanf("%d%d%d",&a,&b,&c); map[a][b] = map[b][a] = c; } bellman_ford(1,n); printf("%d\n",dis[n]); } return 0; }