HDU 4463 Outlets
几乎是裸的最小生成树,只是Nick 和 Apple 要直接相连。
上代码:
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 5 #include<math.h> 6 #include<algorithm> 7 8 #define repA(p,q,i) for(int (i)=(p); (i)!=(q); ++(i) ) 9 #define repD(p,q,i) for(int (i)=(p); (i)!=(q); --(i) ) 10 #define repAE(p,q,i) for(int (i)=(p); (i)<=(q); ++(i) ) 11 #define repDE(p,q,i) for(int (i)=(p); (i)>=(q); --(i) ) 12 #define range 60 13 14 int n,p,q; 15 bool confirm[range]; 16 double maps[range][range]; 17 double loc[range][2]; 18 19 double Dijstra(); 20 21 int main() 22 { 23 while( scanf("%d",&n) != EOF ) 24 { 25 if(n==0) break; 26 scanf("%d%d",&p,&q); 27 repAE(1,n,i) 28 scanf("%lf%lf",&loc[i][0],&loc[i][1]); 29 30 repAE(1,n-1,i) 31 repAE(i,n,j) 32 maps[i][j]=maps[j][i]= sqrt( (loc[i][0]-loc[j][0])*(loc[i][0]-loc[j][0] ) + \ 33 (loc[i][1]-loc[j][1])*(loc[i][1]-loc[j][1]) ); 34 printf("%.2lf\n",Dijstra() ); 35 } 36 return 0; 37 } 38 39 double Dijstra() 40 { 41 repAE(1,n,i) 42 confirm[i]=true; 43 44 double total = maps[p][q]; 45 confirm[p]=confirm[q]=false; 46 repAE(1,n,i) 47 if(confirm[i] && maps[q][i] < maps[p][i]) 48 maps[p][i]=maps[q][i]; 49 50 double minx; 51 int next; 52 int round=n-2; 53 while(round--) 54 { 55 minx = 0x3f3f3f3f; 56 repAE(1,n,i) 57 if(confirm[i]) 58 if(maps[p][i] < minx) 59 { 60 next=i; 61 minx = maps[p][i]; 62 } 63 64 total += maps[p][next]; 65 confirm[next]=false; 66 repAE(1,n,i) 67 if(confirm[i] && maps[next][i] < maps[p][i]) 68 maps[p][i] = maps[next][i]; 69 } 70 return total; 71 }
To Be The Best Of Yourself