floyd 算法

#include<stdio.h>
int n,m,map[124][124];
int inf = 0x7fffffff;
void floyd(  )
{
     for( int i = 1; i <= n; ++i )
          for( int j = 1; j <= n; ++j )
               for( int k = 1; k <= n; ++k )
                    if( map[i][j] != inf && map[i][k] != inf )
                        if( map[i][j] + map[i][k] < map[j][k] )
                            map[j][k] = map[i][j] + map[i][k];
 }
int main( )
{
    while( scanf( "%d%d",&n,&m ),n&&m )
    {
           for( int i = 1; i <= n; ++i )
                for( int j = 1; j <= n; ++j )
                     map[i][j] = inf;
           for( int i = 1; i <= m; ++i )
           {
                int x,y,v;
                scanf( "%d%d%d",&x,&y,&v );
                if( map[x][y] > v )
                    map[x][y] = map[y][x] = v;
                }
           floyd(  );
           printf( "%d\n",map[1][n] );
           }
    return 0;
}
posted @ 2011-08-11 17:14  bcy  阅读(122)  评论(0编辑  收藏  举报