【LA 5713 】 Qin Shi Huang's National Road System (MST)
【题意】
秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最大。问A/B最大是多少?(1000个城市)
Input
The first line contains an integer t meaning that there are t test cases (t ≤ 10).
For each test case:
The first line is an integer n meaning that there are n cities (2 < n ≤ 1000).
Then n lines follow. Each line contains three integers X, Y and P (0 ≤ X, Y ≤ 1000, 0 < P <
100000). (X, Y ) is the coordinate of a city and P is the population of that city.
It is guaranteed that each city has a distinct location.
Output
For each test case, print a line indicating the above mentioned maximum ratio A/B. The result should
be rounded to 2 digits after decimal point.
Sample Input
2
4
1 1 20
1 2 30
200 2 80
200 1 100
3
1 1 20
1 2 30
2 2 40
Sample Output
65.00
70.00
【分析】
枚举法术边,然后目标就是剩下的路长度最小,并且要形成生成树。
普通的做法就是每一次枚举都做一遍,那样就是n^3(排序可以只排一次)。但是其实可以预处理之后再求的。就是一条边置为0之后的最小生成树,这棵树与删边加一起一定构成一个环,我们用这条边代替一条边,必然选择这个环的最大边,预处理maxcost[u][v]表示u,v在生成树上的路径的最大边,然后直接求ans就好了啦~
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 1100
#define Maxm 1000010 struct node
{
int x,y,next;
double c;
};
node t[Maxn*],tt[Maxn],t2[Maxm];
int len,ln,n; int first[Maxn],fa[Maxn];
double mc[Maxn][Maxn];
bool np[Maxn]; int myabs(int x) {return x<?-x:x;}
// int mymax(int x,int y) {return x>y?x:y;}
double mymax(double x,double y) {return x>y?x:y;} void ins(int x,int y,double c)
{
t[++ln].x=x;t[ln].y=y;t[ln].c=c;
t[ln].next=first[x];first[x]=ln;
} bool cmp(node x,node y) {return x.c<y.c;} int ffind(int x)
{
if(fa[x]!=x) fa[x]=ffind(fa[x]);
return fa[x];
} void dfs(int x,int f,double l)
{
for(int i=;i<=n;i++) if(np[i])
mc[i][x]=mc[x][i]=mymax(mc[i][f],l);
np[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
dfs(t[i].y,x,t[i].c);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d%lf",&tt[i].x,&tt[i].y,&tt[i].c);
len=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
double nx=(double)(tt[i].x-tt[j].x),ny=(double)(tt[i].y-tt[j].y);
t2[++len].x=i;t2[len].y=j;t2[len].c=sqrt(nx*nx+ny*ny);
// ins(i,j,sqrt(nx*nx+ny*ny));
}
for(int i=;i<=n;i++) fa[i]=i;
double sum=;
ln=;memset(first,,sizeof(first));
sort(t2+,t2++len,cmp);
for(int i=;i<=len;i++)
{
if(ffind(t2[i].x)!=ffind(t2[i].y))
{
fa[ffind(t2[i].x)]=ffind(t2[i].y);
ins(t2[i].x,t2[i].y,t2[i].c);ins(t2[i].y,t2[i].x,t2[i].c);
sum+=t2[i].c;
}
if(ln==*(n-)) break;
}
memset(mc,,sizeof(mc));
memset(np,,sizeof(np));
dfs(,,);
double ans=;
for(int i=;i<=len;i++)
{
double A=tt[t2[i].x].c+tt[t2[i].y].c;
if(A*1.0/(sum-mc[t2[i].x][t2[i].y])>ans)
ans=A*1.0/(sum-mc[t2[i].x][t2[i].y]);
}
printf("%.2lf\n",ans);
}
return ;
}
2016-10-31 21:44:35
【LA 5713 】 Qin Shi Huang's National Road System (MST)的更多相关文章
- 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: 32768/3 ...
- UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest
同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...
- UVALive-5713 Qin Shi Huang's National Road System (次小生成树)
题目大意:有n个城市,要修一些路使得任意两个城市都能连通.但是有人答应可以不计成本的帮你修一条路,为了使成本最低,你要慎重选择修哪一条路.假设其余的道路长度为B,那条别人帮忙修的道路两端城市中的总人口 ...
- hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
- HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- UValive 5713 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 ...
- 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: 32768/3 ...
随机推荐
- linux 软连接方式实现上传文件存储目录的无缝迁移
背景: 由于前期的磁盘空间规划与后期的业务要求不符合.原先/home被用于用户上传文件的存储目录,但是由于上传文件的逐渐增多,而原来的/home目录的空间不足,需要给/home目录进行扩容.同时各个应 ...
- 基于Windows的套接字相关函数及示例
链接ws2_32.lib库 头文件#include <winsock2.h> int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSADa ...
- FPGA异步时钟设计中的同步策略
1 引言 基于FPGA的数字系统设计中大都推荐采用同步时序的设计,也就是单时钟系统.但是实际的工程中,纯粹单时钟系统设计的情况很少,特别是设计模块与外围芯片的通信中,跨时钟域的情况经常不可避免. ...
- C#迭代语句
1,do while语句 do语句重复执行语句或者语句块,直到指定的表达式为false为止.循环体如果为单个语句,可以不放在{}内,如果不是,那么必须放在{}内.如下面的代码 Codeint i=0; ...
- java中关于时间的格式化
long time = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat(); String s = ...
- c#接口深入一步探究其作用,适合新人了解
前言 前一篇浅显的述说了一下c#接口的作用,并用了一个不怎么好的例子述说了一下.时隔一天,在看完大家的评论后我在论坛中查看了很多前辈们对c#接口的描述,发现大家对例子的说明不是太容易让我们这些新人理解 ...
- 网卡添加VLAN TAG
#modprobe 8021q 用命令 lsmod | grep 8021q 来检查 以配置网卡eth0为例,添加vlan号:1002 ================================ ...
- Leaflet交流
GIS科研网 Leaflet交流 谢绝转载 http://www.3sbase.com欢迎加群交流 108299288 http://www.3sbase.com/3sbase/webgistest ...
- SGU 260.Puzzle (异或高斯消元)
题意: 有n(<200)个格子,只有黑白两种颜色.可以通过操作一个格子改变它和其它一些格子的颜色.给出改变的关系和n个格子的初始颜色,输出一种操作方案使所有格子的颜色相同. Solution: ...
- 九度OJ 1082 代理服务器 -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...