标签:des style blog http io color ar os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3843 Accepted Submission(s): 1336
1 //#define LOCAL 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int maxn=1005; 10 const int inf=0x3f3f3f3f; 11 struct node 12 { 13 int x,y,p; 14 double dist(const node &cc){ 15 return sqrt((double)(x-cc.x)*(x-cc.x)+(y-cc.y)*(y-cc.y)); 16 } 17 }sac[maxn]; 18 19 bool vis[maxn]; 20 bool road[maxn][maxn]; 21 int pre[maxn]; 22 double maxc[maxn][maxn]; 23 double lowcost[maxn]; 24 double map[maxn][maxn]; 25 double res; 26 void prim(int st,int en){ 27 28 memset(vis,0,sizeof(vis)); 29 memset(road,0,sizeof(road)); 30 memset(maxc,0,sizeof maxc); 31 for(int i=0;i<en;i++){ 32 lowcost[i]=map[st][i]; 33 pre[i]=st;; 34 } 35 vis[st]=1; 36 res=0; 37 for(int i=0;i<en;i++) 38 { 39 double larger=inf; 40 int pp=-1; 41 for(int j=0;j<en;j++) 42 { 43 if(!vis[j]&&larger>lowcost[j]) 44 { 45 larger=lowcost[j]; 46 pp=j; 47 } 48 } 49 if(-1==pp)continue; 50 road[pp][pre[pp]]=road[pre[pp]][pp]=1; 51 res+=lowcost[pp]; 52 vis[pp]=1; 53 for(int i=0;i<en;i++) 54 { 55 56 if(!vis[i]&&lowcost[i]>map[pp][i]){ 57 lowcost[i]=map[pp][i]; 58 pre[i]=pp; 59 } 60 //求解生成树的最大边 61 if(vis[i]&&i!=pp){ 62 maxc[i][pp]=maxc[pp][i]=max(maxc[i][pre[pp]],lowcost[pp]); 63 } 64 } 65 } 66 return ; 67 } 68 69 int main() 70 { 71 #ifdef LOCAL 72 freopen("test.in","r",stdin); 73 #endif 74 int tt,nn; 75 scanf("%d",&tt); 76 while(tt--){ 77 scanf("%d",&nn); 78 // memset(map,0,sizeof(map)); 79 for(int i=0;i<nn;i++){ 80 scanf("%d%d%d",&sac[i].x,&sac[i].y,&sac[i].p); 81 map[i][i]=0; 82 for(int j=i-1;j>=0;--j){ 83 map[i][j]=map[j][i]=sac[i].dist(sac[j]); 84 } 85 } 86 prim(0,nn); 87 double ans=0.0; 88 for(int i=0;i<nn;i++){ 89 for(int j=0;j<nn;j++){ 90 if(i!=j) 91 { 92 double tol_p=sac[i].p+sac[j].p; 93 if(road[i][j]) 94 ans=max(tol_p/(res-map[i][j]),ans); 95 else 96 ans=max(tol_p/(res-maxc[i][j]),ans); 97 } 98 } 99 } 100 printf("%.2lf\n",ans); 101 } 102 return 0; 103 }
hdu 4081 Qin Shi Huang's National Road System (次小生成树)
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/gongxijun/p/4099934.html