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

hdu 4081 Qin Shi Huang's National Road System

时间:2015-03-16 22:40:12      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

思路: 建立最小生成树的图,之后进行枚举边(两点)。然后去掉原来生成树上的边。 去掉一条遍之后,原图为两棵树。

A - 分别为两棵树中的最大人口和

B - 生成树边长之和减去一条生成树边的长度。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<string>
  6 #include<queue>
  7 #include<algorithm>
  8 #include<map>
  9 #include<iomanip>
 10 #include<climits>
 11 #include<string.h>
 12 #include<cmath>
 13 #include<stdlib.h>
 14 #include<vector>
 15 #include<stack>
 16 #include<set>
 17 #define INF 1e7
 18 #define MAXN 10010
 19 #define maxn 1000010
 20 #define Mod 1000007
 21 #define N 1010
 22 using namespace std;
 23 typedef long long LL;
 24 
 25 struct node{
 26     double x, y, num;
 27 }p[N];
 28 double G[N][N], dis[N], A, B;
 29 vector<int> vec[N];
 30 int vis[N];
 31 int T, n;
 32 int x, y;
 33 int pre[N];
 34 
 35 double getdis(node a, node b)
 36 {
 37     return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
 38 }
 39 
 40 void prim()            //对最小生成树建图
 41 {
 42     int now, pos;
 43     double min;
 44     for (int i = 1; i <= n; ++i) dis[i] = INF;
 45     now = 1;
 46     for (int i = 1; i < n; ++i) {
 47         min = INF;
 48         for (int j = 1; j <= n; ++j) {
 49             if (dis[j] > G[now][j])
 50                 dis[j] = G[now][j], pre[j] = now;
 51         }
 52         dis[now] = -1;
 53         for (int j = 1; j <= n; ++j) {
 54             if (dis[j] > 0 && min > dis[j])
 55                 min = dis[j], pos = j;
 56         }
 57         vec[pre[pos]].push_back(pos);
 58         vec[pos].push_back(pre[pos]);
 59         B += min;
 60         now = pos;
 61     }
 62 }
 63 
 64 int dfs(int u, int fa)
 65 {
 66     int max_num = p[u].num;
 67     int ret = u, tt;
 68     for (int i = 0; i < vec[u].size(); ++i) {
 69         int v = vec[u][i];
 70         if (v == fa) continue;
 71         tt = dfs(v, u);
 72         if (max_num < p[tt].num)
 73             max_num = p[tt].num, ret = tt;
 74     }
 75     return ret;
 76 }
 77 
 78 void run()
 79 {
 80     cin >> n;
 81     for (int i = 1; i <= n; ++i)
 82         cin >> p[i].x >> p[i].y >> p[i].num;
 83     for (int i = 1; i <= n; ++i)
 84         for (int j = 1; j <= n; ++j)
 85         {
 86             if (i == j)  continue;
 87             G[i][j] = G[j][i] = getdis(p[i], p[j]);
 88         }
 89     for (int i = 0; i <= n; ++i) vec[i].clear();
 90     B = 0;
 91     memset(pre,-1,sizeof(pre));
 92     prim();
 93     double ans = -1.0, res;
 94     int t1, t2;
 95     for (int i = 1; i <= n; ++i)
 96         for (int j = 0; j < vec[i].size(); ++j) {
 97             int v = vec[i][j];
 98             t1 = dfs(i, v);
 99             t2 = dfs(v, i);
100             res = (p[t1].num + p[t2].num)/(B - G[i][v]);
101             if (res > ans) ans = res;
102         }
103     printf("%.2lf\n",ans);
104 }
105 
106 int main()
107 {
108     cin >> T;
109     while (T--) {
110         run();
111     }
112     return 0;
113 }

 

hdu 4081 Qin Shi Huang's National Road System

标签:

原文地址:http://www.cnblogs.com/usedrosee/p/4342922.html

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