题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2544
原来用dijkstra算法提交过,这是用floyd_warshall算法写的!
#include <stdio.h> #define INF 1000000 #define MAXN 110 int d[MAXN][MAXN]; int main() { int n, m, i, j, k; int temp; while( (scanf("%d%d", &n, &m)!= EOF) && n && m ) { for( i = 1; i <= n; i++ ) for( j = 1; j <= n; j++ ) if( i != j ) d[i][j] = INF; else d[i][j] = 0; for( k = 0; k < m; k++ ) { scanf( "%d%d%d", &i, &j, &temp ); if( d[i][j] > temp ) d[i][j] = d[j][i] = temp; } //floyd_warshall算法 for( k = 1; k <= n; k++ ) for( i = 1; i <= n; i++ ) for( j = 1; j <= n; j++ ) if( d[i][j] > d[i][k] + d[k][j] ) d[i][j] = d[i][k] + d[k][j]; printf( "%d ", d[1][n] ); } }