UVA 1494 - Qin Shi Huang's National Road System(次小生成树)
题目链接:点击打开链接
题意: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(次小生成树)相关推荐
- 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 ...
- 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 ...
- HDU 4081 Qin Shi Huang's National Road System (次小生成树算法)
转载自http://blog.csdn.net/shuangde800 D_Double 题目: Problem Description During the Warring States Perio ...
- HDU - 4081 Qin Shi Huang's National Road System(次小生成树)
题目大意:有N个城市,现在要修建一些道路使得这些城市能互相连通,修建一条道路的权值为这两个城市之间的欧几里德距离. 现在你可以选择一条路作为特殊道路,这条道路的权值为0 假设修建道路的总权值为B,用特 ...
- Qin Shi Huang's National Road System HDU - 4081
Qin Shi Huang's National Road System (HDU - 4081) 次小生成树的思想 题目描述 During the Warring States Period of ...
- 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 ...
- Qin Shi Huang's National Road System( 次小生成树 )
Qin Shi Huang's National Road System( 次小生成树 ) During the Warring States Period of ancient China(476 ...
- 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 ...
- Qin Shi Huang's National Road System
Qin Shi Huang's National Road System 题目大意: Problem Description During the Warring States Period of a ...
最新文章
- php正则匹配td,正则表达式-查找td标签之间的所有内容 - php
- cadence一些术语
- IDEA Scala自动生成变量类型设置教程
- java 连接kafka超时_java – Kafka KStreams – 处理超时
- 【洛谷4001】 [ICPC-Beijing 2006]狼抓兔子(最小割)
- 搭建MSSM框架(Maven+Spring+Spring MVC+MyBatis)
- 如何使用 SQL Server FILESTREAM 存储非结构化数据?这篇文章告诉你!
- 使用Python完美管理和调度你的多个任务
- 阿里云RDB-MySQL5.7数据库:Specified key was too long; max key length is 767 bytes;
- svn 切换账号信息
- java udp 流量控制_基于UDP传输协议的实现分析之流量和拥塞控制
- 战斗部毁伤效能评估软件系统
- 如何写一份大家都满意的专利说明书
- PS使用技巧(一) 移动工具 V
- 如何让在 LaTeX 中添加脚注,并且文中不出现编号?
- skype安卓手机版_安卓手机版电视直播播放器
- CentOS7修改Locale为zh_CN.UTF-8
- 2017-11-18 借白银说点市场心得
- 事件冒泡 vs 事件捕获 vs 事件委托 的区别
- 计算机控制台程序,什么是电脑控制台,是DOS设定吗?