HDU4081

转载解析:https://blog.csdn.net/shuangde800/article/details/7982106

下面是Kruscal的写法,比较丑

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<string.h>
  4 #include<math.h>
  5 #include<vector>
  6 using namespace std;
  7 struct node{
  8     double x,y;
  9     int p;
 10 }a[1001];
 11 
 12 struct node1{
 13     int to;
 14     double w;
 15 };
 16 
 17 struct edge{
 18     int num;
 19     int from,to;
 20     double w;
 21     int cost;
 22     bool operator < (const edge &a){
 23         return w<a.w;
 24     }
 25 }e[1001000];
 26 
 27 int book[1001000],f[1001],pbook[1001];
 28 int q,n,m,p;
 29 double x,y,ans,mst,path[1001][1001];
 30 vector<node1> vec[1001];
 31 
 32 double dis(const node &a ,const node &b){
 33     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 34 }
 35 
 36 void ini(){
 37     memset(book,0,sizeof book);
 38     memset(pbook,0,sizeof pbook);
 39     memset(path,0,sizeof path);
 40     for (int i=1;i<=n;i++) f[i]=i;
 41     m=0;
 42     ans=-1;
 43     mst=0;
 44     for (int i=0;i<1001;i++) vec[i].clear();
 45 }
 46 
 47 int getf(int u){
 48     return f[u]==u?f[u]:f[u]=getf(f[u]);
 49 }
 50 
 51 void merge(int u,int v){
 52     f[getf(u)]=getf(v);
 53 }
 54 
 55 void dfs(int now,int root){
 56     if (pbook[now]) return;
 57     pbook[now]=1;
 58     for (node1 nx:vec[now]){
 59         if (pbook[nx.to]) continue;
 60         path[root][nx.to]=max(path[root][now],nx.w);
 61         dfs(nx.to,root);
 62     }
 63 }
 64 
 65 int main(){
 66     scanf("%d",&q);
 67     while (q--){
 68         scanf("%d",&n);
 69         ini();
 70         for (int i=1;i<=n;i++){
 71             scanf("%lf %lf %d",&x,&y,&p);
 72             a[i]={x,y,p};
 73             for (int j=1;j<=i-1;j++){
 74                 m++;
 75                 e[m]={m,i,j,dis(a[i],a[j]),a[i].p+a[j].p};
 76             }
 77         }
 78         sort(e+1,e+1+m);
 79         for (int i=1;i<=m;i++){
 80             if (getf(e[i].from)!=getf(e[i].to)){
 81                 mst+=e[i].w;
 82                 merge(e[i].from,e[i].to);
 83                 book[e[i].num]=1;
 84                 vec[e[i].from].push_back({e[i].to,e[i].w});
 85                 vec[e[i].to].push_back({e[i].from,e[i].w});
 86             }
 87         }
 88         //cout<<mst<<endl;
 89         for (int i=1;i<=n;i++){
 90             memset(pbook,0,sizeof pbook);
 91             dfs(i,i);
 92         }
 93         /*
 94         for (int i=1;i<=n;i++){
 95             for (int j=1;j<=n;j++) cout<<path[i][j]<<" ";
 96             cout<<endl;
 97         }
 98         */
 99         for (int i=1;i<=m;i++){
100             if (book[e[i].num]) ans=max(ans,e[i].cost/(mst-e[i].w));
101             else ans=max(ans,e[i].cost/(mst-path[e[i].from][e[i].to]));
102         }
103         printf("%.2f\n",ans);
104     }
105     return 0;
106 }

 

posted @ 2020-05-13 19:49  White_Li  阅读(123)  评论(0)    收藏  举报