HDU3665
最短路
floyd
View Code
1 #include<stdio.h> 2 #include<string.h> 3 const int maxn =11; 4 int sea[ maxn ]; 5 int mat[ maxn ][ maxn ]; 6 const int inf=999999; 7 8 void init(){ 9 memset( sea,0,sizeof(sea)); 10 for( int i=0;i<maxn;i++ ) 11 for( int j=0;j<maxn;j++ ) 12 mat[ i ][ j ]=inf; 13 } 14 15 int main(){ 16 int n; 17 while( scanf("%d",&n)!=EOF ){ 18 init(); 19 for( int i=0;i<n;i++ ){ 20 int a,b; 21 scanf("%d%d",&a,&b); 22 sea[ i ]=b; 23 while( a-- ){ 24 int pos,dis; 25 scanf("%d%d",&pos,&dis); 26 mat[ i ][ pos ]=dis; 27 } 28 } 29 30 for( int k=0;k<n;k++ ){ 31 for( int i=0;i<n;i++ ){ 32 for( int j=0;j<=n;j++ ){ 33 if( mat[ i ][ k ]!=inf && mat[ k ][ j ]!=inf && mat[i][j]>(mat[i][k]+mat[k][j]) ){ 34 mat[i][j]=(mat[i][k]+mat[k][j]); 35 } 36 } 37 } 38 } 39 40 int ans1=inf; 41 for( int i=1;i<n;i++ ){ 42 if( ans1>mat[0][i] && sea[i]!=0 && mat[0][i]!=inf ){ 43 ans1=mat[0][i]; 44 } 45 } 46 47 printf("%d\n",ans1); 48 } 49 return 0; 50 }
keep moving...