区域赛的水题。。。题意理解好了就OK了,模版题,还是调试了一会,自己对代码不太熟练啊。
1 #include <stdio.h> 2 #include <string.h> 3 #define N 1000000 4 int main() 5 { 6 int p[20][20],low[13],o[13],sea[13]; 7 int i,j,k,n,a,b,w,ev,min; 8 while(scanf("%d",&n)!=EOF) 9 { 10 memset(o,0,sizeof(o)); 11 memset(sea,0,sizeof(sea)); 12 for(i = 0;i <= n-1;i ++) 13 for(j = 0;j <= n-1;j ++) 14 { 15 if(i!=j) 16 p[i][j] = N; 17 else 18 p[i][j] = 0; 19 } 20 for(i = 0;i <= n-1;i ++) 21 { 22 scanf("%d%d",&a,&b); 23 if(b == 1) sea[i] = 1; 24 for(j = 1;j <= a;j ++) 25 { 26 scanf("%d%d",&ev,&w); 27 if(p[i][ev] > w) 28 p[i][ev] = w; 29 if(p[ev][i] > w) 30 p[ev][i] = w; 31 } 32 } 33 for(i = 0;i <= n-1;i ++) 34 { 35 low[i] = p[0][i]; 36 } 37 o[0] = 1; 38 for(i = 1;i <= n-2;i ++) 39 { 40 min = N; 41 for(j = 0;j <= n-1;j ++) 42 { 43 if(min > low[j]&&o[j] == 0) 44 { 45 min = low[j]; 46 k = j; 47 } 48 } 49 o[k] = 1; 50 for(j = 0;j <= n-1;j ++) 51 { 52 if(o[j] == 0&&low[j] > min + p[k][j]) 53 { 54 low[j] = p[k][j]+min; 55 } 56 } 57 } 58 min = N; 59 for(i = 0;i <= n-1;i ++) 60 { 61 if(min > low[i]&&sea[i]) 62 { 63 min = low[i]; 64 } 65 } 66 printf("%d\n",min); 67 } 68 return 0; 69 }