floyd算法(hdu2544)
#include <cstdio>
const int N = 105 ;
const int INF = 1e6 ; //路口间的初始距离,看成无穷大,相当于断开
int graph[N][N] ;
int n , m ;
void floyd(){
int s = 1 ;
for (int k = 1 ; k <= n ; k ++){ //遍历每一个点作为中转点
for (int i = 1 ; i <= n ; i ++){
if (graph[i][k] != INF){
for (int j = 1 ; j <= n ; j ++)
if (graph[i][j] > graph[i][k] + graph[k][j])
graph[i][j] = graph[i][k] + graph[k][j] ;
}
}
}
printf ("%d\n",graph[s][n]);
}
int main(){
while(~scanf ("%d%d",&n,&m)){
if (n == 0 && m == 0) return 0 ;
for (int i = 1 ; i <= n ; i ++)
for (int j = 1 ; j <= n ; j ++)
graph[i][j] = INF ;
int a , b , c ;
while(m --){
scanf ("%d%d%d",&a,&b,&c) ;
graph[a][b] = c ;
graph[b][a] = c ; //邻接矩阵存图
}
floyd() ;
}
return 0 ;
}