【题意】

  秦始皇要在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)的更多相关文章

  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: 32768/3 ...

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

    题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...

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

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

  4. hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest

    同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...

  5. UVALive-5713 Qin Shi Huang's National Road System (次小生成树)

    题目大意:有n个城市,要修一些路使得任意两个城市都能连通.但是有人答应可以不计成本的帮你修一条路,为了使成本最低,你要慎重选择修哪一条路.假设其余的道路长度为B,那条别人帮忙修的道路两端城市中的总人口 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Ubuntu 14.04 LAMP搭建小记

    文章目录 LAMP WinQQ Ubuntu 的使用的建模工具 JDK Chormium flash Eclipse 无法找到Jre LAMP 参考资料: 1. 安装php环境   http://ww ...

  2. Topself

    TopShelf简介 个人理解:开源.跨平台的服务框架.提供一种方式以控制台编写windows服务,与windows服务相比,目前只发现便于调试. 官网网站:http://docs.topshelf- ...

  3. Magento Block设计分析(深入分析)

    Magento中Block是一个很重要的组件,它在Block中充当非常重要的角色,下面我们来分析一下Magento中Block是怎样设计的,我们应该怎样使用这个重要的角色. 1.Magento Blo ...

  4. JQ 如何设置单选按钮问题

    <input type="radio" name="db_12" value="2" checked="checked/&g ...

  5. hibernate篇章三-- hibernate配置文件hibernate.cfg.xml的详细解释

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...

  6. C# 调用Java Webservice 加入SoapHeader 验证信息

    C#调用java 编写的webservice时不会自动生成 soapheader 类接口的,需要改动Reference.cs. 在生成的代理类referende.cs中进行如下操作: 一.在声明pub ...

  7. 双程动态规划 nyoj61

    题目大意: 在矩阵m*n中,从(1,1)点到(m,n)点,再从(m,n)点到(1,1)点,所走路线经过的同学最大好心值, 要求每个点只能走一遍. 分析: ①我们可以把它只看成两个人同时从(1,1)点, ...

  8. 冒泡排序算法(C#实现)

    简单的冒泡排序算法,代码如下: ] = temp; hasExchangeAction =true; //发生过互换 } } if (!hasExchangeAction) //如果没有发生过互换,则 ...

  9. 解决 cocoapods diff: /../Podfile.lock: No such file or directory 问题

    解决cocoapods diff: /../Podfile.lock: No such file or directory google一圈之后,找到两个解决方案: 方案一:
 关闭Xcode,重新执 ...

  10. iOS .pch文件的使用

    什么是.pch文件预编译头文件(一般扩展名为.PCH),是把一个工程中较稳定的代码预先编译好放在一个文件(.PCH)里.这些预先编译好的代码可以是任何的C/C++代码--甚至可以是inline函数,只 ...