hdu 2680 Choose the best route
#include<stdio.h> int map[1024][1024],des[1024],dis[1024],s,w; int m,n,inf = 0x7fffffff,q; int Dijkstra( ) { dis[0] = 0; for( int i = 0; i <= n; ++i ) { int min = inf,t = 0,pos = -1; for( int j = 0; j <= n; ++j ) { if( !des[j] ) if( dis[j] < min ) { min = dis[j]; pos = j; } } des[pos] = 1; if( des[s] ) return dis[s]; if( min == inf )//我一直wa所在地 return -1; for( int j = 0; j <= n; ++j ) { if( !des[j] ) if( map[pos][j] != inf ) if( ( t = map[pos][j] + dis[pos] ) < dis[j] ) dis[j] = t; } } } int main( ) { while( scanf( "%d%d%d",&n,&m,&s ) != EOF ) { int res = 1; for( int i = 0; i <= n + 5; ++i ) { for( int j = 0; j <= n + 5; ++j ) map[i][j] = inf; des[i] = 0; dis[i] = inf; } for( int i = 1; i <= m; ++i ) { int x,y,val; scanf( "%d%d%d",&x,&y,&val ); if( val < map[x][y] ) map[x][y] = val; } scanf( "%d",&w ); for( int i = 1; i <= w; ++i ) { scanf( "%d",&q ); map[0][q] = 0; } res = Dijkstra(); printf( "%d\n",res ); } return 0; }
此题开始我一直wa后来看了大牛的代码才知道把单向弄成双向了,但是仍然wa,后来又仔细看大牛代码,终于看出-1所在地不同,好吧,果断改了,水过
本人还是新手 ,转载请注明来自Lvsi‘s home