题目链接:点击打开链接

题意:n个城市,需要修建一些道路使得任意两个城市联通,还可以修一条魔法道路, 不花钱, 设魔法路连接的城市的人口之和为A, 所有道路总长为B, 求A/B的最大值。

思路: 次小生成树。 先做一次最小生成树, 然后用dfs搜索出最小生成树上任意两点之间最长的道路长度。  然后枚举在哪两个城市之间建魔法道路。n^2复杂度。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 1000 + 10;
int T,n,m,cnt,p[maxn];
double val[maxn][maxn],d[maxn][maxn];
bool vis[maxn];
struct node {double x, y, p;
}a[maxn];
struct Edge {int a, b;double c;Edge(int a=0,int b=0,double c=0):a(a), b(b), c(c) {}bool operator < (const Edge& rhs) const {return c < rhs.c;}
}edges[maxn*maxn];
vector<int> g[maxn];
int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }
void dfs(int root, int cur) {vis[cur] = true;int len = g[cur].size();for(int i=0;i<len;i++) {int v = g[cur][i];if(!vis[v]) {val[root][v] = max(val[root][cur], d[cur][v]);dfs(root, v);}}
}
double solve() {sort(edges, edges + cnt);for(int i=1;i<=n;i++) p[i] = i, g[i].clear();double minv = 0;for(int i=0;i<cnt;i++) {int x = _find(edges[i].a);int y = _find(edges[i].b);if(x != y) {p[x] = y;g[edges[i].a].push_back(edges[i].b);g[edges[i].b].push_back(edges[i].a);d[edges[i].a][edges[i].b] = d[edges[i].b][edges[i].a] = edges[i].c;minv += edges[i].c;}}for(int i=1;i<=n;i++) {memset(vis, false, sizeof(vis));dfs(i, i);}double ans = 0;for(int i=1;i<=n;i++) {for(int j=i+1;j<=n;j++) {double A = a[i].p + a[j].p;double B = minv - val[i][j];ans = max(ans, A/B);}}return ans;
}
int main() {scanf("%d",&T);while(T--) {scanf("%d",&n);for(int i=1;i<=n;i++) {scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].p);}cnt = 0;for(int i=1;i<=n;i++) {for(int j=i+1;j<=n;j++) {double dist = 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));edges[cnt++] = Edge(i, j, dist);}}printf("%.2f\n",solve());}return 0;
}

UVA 1494 - Qin Shi Huang's National Road System(次小生成树)相关推荐

  1. HDU - 4081 Qin Shi Huang‘s National Road System(次小生成树)

    点击打开题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  2. hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)

    题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...

  3. HDU 4081 Qin Shi Huang's National Road System (次小生成树算法)

    转载自http://blog.csdn.net/shuangde800 D_Double 题目: Problem Description During the Warring States Perio ...

  4. HDU - 4081 Qin Shi Huang's National Road System(次小生成树)

    题目大意:有N个城市,现在要修建一些道路使得这些城市能互相连通,修建一条道路的权值为这两个城市之间的欧几里德距离. 现在你可以选择一条路作为特殊道路,这条道路的权值为0 假设修建道路的总权值为B,用特 ...

  5. Qin Shi Huang's National Road System HDU - 4081

    Qin Shi Huang's National Road System (HDU - 4081) 次小生成树的思想 题目描述 During the Warring States Period of ...

  6. hdu-4081 Qin Shi Huang's National Road System(次小生成树)

    题目链接:点击打开链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  7. Qin Shi Huang's National Road System( 次小生成树 )

    Qin Shi Huang's National Road System( 次小生成树 ) During the Warring States Period of ancient China(476 ...

  8. HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  9. Qin Shi Huang's National Road System

    Qin Shi Huang's National Road System 题目大意: Problem Description During the Warring States Period of a ...

最新文章

  1. php正则匹配td,正则表达式-查找td标签之间的所有内容 - php
  2. cadence一些术语
  3. IDEA Scala自动生成变量类型设置教程
  4. java 连接kafka超时_java – Kafka KStreams – 处理超时
  5. 【洛谷4001】 [ICPC-Beijing 2006]狼抓兔子(最小割)
  6. 搭建MSSM框架(Maven+Spring+Spring MVC+MyBatis)
  7. 如何使用 SQL Server FILESTREAM 存储非结构化数据?这篇文章告诉你!
  8. 使用Python完美管理和调度你的多个任务
  9. 阿里云RDB-MySQL5.7数据库:Specified key was too long; max key length is 767 bytes;
  10. svn 切换账号信息
  11. java udp 流量控制_基于UDP传输协议的实现分析之流量和拥塞控制
  12. 战斗部毁伤效能评估软件系统
  13. 如何写一份大家都满意的专利说明书
  14. PS使用技巧(一) 移动工具 V
  15. 如何让在 LaTeX 中添加脚注,并且文中不出现编号?
  16. skype安卓手机版_安卓手机版电视直播播放器
  17. CentOS7修改Locale为zh_CN.UTF-8
  18. 2017-11-18 借白银说点市场心得
  19. 事件冒泡 vs 事件捕获 vs 事件委托 的区别
  20. 计算机控制台程序,什么是电脑控制台,是DOS设定吗?

热门文章

  1. python代码给任意手机号发信息
  2. STM32构建BootLoader实现多系统OTA
  3. uniapp页面回到顶部方法
  4. FPGA实现计时器功能(黑金开发平台)
  5. Windows 7,win7,旗舰版,家庭版,企业版等版本主要区别
  6. JCVideoPlayer播放器的用法
  7. 《张爱玲传:失望是一种幸福》——李燕【中】读后感
  8. ubuntu搭建基于arm-none-eabi-gcc,jlink驱动的平台,并实现stm32f407zgt6程序的编译链接和烧写
  9. WMS中RestSharp的使用
  10. MATLAB图像残差像素求均值