码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4081 Qin Shi Huang's National Road System

时间:2015-07-25 13:35:34      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

这个题还是比较好的,提供了一种求最小生成树不错的方法,但是,C++ TLE,G++  400+ms

  1 #include <cmath>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 const int maxn = 1005;
  7 struct edge
  8 {
  9     int v,next;
 10     double w;
 11 }e[maxn*2];
 12 int head[maxn],cnt;
 13 double d[maxn][maxn];
 14 bool vis[maxn*maxn];
 15 int fa[maxn];
 16 struct EDGE
 17 {
 18     int u,v,p;
 19     double w;
 20 }E[maxn*maxn];
 21 struct node
 22 {
 23     double x,y;
 24     int p;
 25 }a[maxn];
 26 double dis(int i,int j)
 27 {
 28     return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
 29 }
 30 bool cmp(EDGE a,EDGE b)
 31 {
 32     return a.w<b.w;
 33 }
 34 void add(int u,int v,double w)
 35 {
 36     e[cnt].v = v;
 37     e[cnt].w = w;
 38     e[cnt].next = head[u];
 39     head[u] = cnt++;
 40 }
 41 int findd(int x)
 42 {
 43     int rt = x;
 44     while(rt!=fa[rt])rt = fa[rt];
 45     while(x!=rt)
 46     {
 47         int t = fa[x];
 48         fa[x] = rt;
 49         x = t;
 50     }
 51     return rt;
 52 }
 53 int root;
 54 void dfs(int rt,int anc,double dist)
 55 {
 56     d[root][rt] = max(d[root][anc],dist);
 57     for(int i = head[rt];i!=-1;i = e[i].next)
 58         if(e[i].v!=anc)
 59             dfs(e[i].v,rt,e[i].w);
 60 }
 61 int main()
 62 {
 63    // freopen("in.txt","r",stdin);
 64     int T;scanf("%d",&T);
 65     while(T--)
 66     {
 67         int n;scanf("%d",&n);
 68         for(int i = 1;i<=n;++i)scanf("%lf%lf%d",&a[i].x,&a[i].y,&a[i].p);
 69         int m = 0;
 70         for(int i = 1;i<=n;++i)
 71             for(int j = i+1;j<=n;++j)
 72             {
 73                 ++m;
 74                 E[m].u = i;
 75                 E[m].v = j;
 76                 E[m].w = dis(i,j);
 77                 E[m].p = a[i].p+a[j].p;
 78             }
 79         sort(E+1,E+1+m,cmp);
 80         for(int i = 1;i<=n;++i)fa[i] = i;
 81         memset(head,-1,sizeof(head));
 82         for(int i = 1;i<=n;++i)head[i] = -1;
 83         for(int i = 1;i<=m;++i)vis[i] = 0;
 84         cnt = 0;
 85         double MST = 0;
 86         for(int i = 1;i<=m;++i)
 87         {
 88             int fx = findd(E[i].u),fy = findd(E[i].v);
 89             if(fx==fy)continue;
 90             add(E[i].u,E[i].v,E[i].w);
 91             add(E[i].v,E[i].u,E[i].w);
 92             fa[fx] = fy;
 93             vis[i] = 1;
 94             MST += E[i].w;
 95         }
 96         for(int i = 1;i<=n;++i)
 97         {
 98             root = i;
 99             d[root][i] = 0;
100             dfs(i,i,0);
101         }
102         double ans = 0;
103         for(int i = 1;i<=m;++i)
104         {
105             if(vis[i])ans = max(ans,(double)E[i].p/(MST-E[i].w));
106             else ans = max(ans,(double)E[i].p/(MST-d[E[i].u][E[i].v]));
107         }
108         printf("%.2lf\n",ans);
109     }
110     return 0;
111 }

 

HDU4081 Qin Shi Huang's National Road System

标签:

原文地址:http://www.cnblogs.com/GJKACAC/p/4675616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!