杭电 2544 最短路(floyd_warshall)

题目地址:

  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\n", d[1][n] );

    }
}
View Code

 

posted @ 2013-08-01 14:39  翼展zjz  阅读(207)  评论(0编辑  收藏  举报