【题意】

  秦始皇要在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. js中的"=="与"==="的区别

    "==": 1,如果两表达式的类型不同,则试图将它们转换为字符串.数字或 Boolean 量. 2,NaN 与包括其本身在内的任何值都不相等. 3,负零等于正零. 4,null 与 ...

  2. ruby gem源更换国内源gems.ruby-china.org数据源

    gem sources -l gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ 更新缓存 ge ...

  3. C# HTTP 请求

    public class HttpHelper { /// <summary> /// 创建GET方式的HTTP请求 /// </summary> public static ...

  4. 使用ASP.NET实现Windows Service定时执行任务

    转载http://blog.csdn.net/yanghua_kobe/article/details/6937816 我们怎样才能在服务器上使用asp.net定时执行任务而不需要安装windows ...

  5. 20160330javaweb之session 小练习

    练习一:session 实现登录注销 package com.dzq.session.logout; import java.util.*; public class UserDao { /** * ...

  6. pull解析xml文件

    pull解析xml文件 先自己写一个xml文件,存一些天气信息 拿到xml文件 InputStream is = getClassLoader().getResourceAsStream(" ...

  7. ios Toll-Free Bridging

    有一些数据类型是能够在 Core Foundation Framework 和 Foundation Framework 之间交换使用的.这意味着,对于同一个数据类型,你既可以将其作为参数传入 Cor ...

  8. mybatis 打印 sql

    该文中使用的log框架为logback myBatis3.0.6左右的版本时 打印sql的时候只需要配置如下属性: <logger name="java.sql.Connection& ...

  9. iOS开发——UIWebView

    (已写好代码,待空闲更新……)

  10. iOS9 UITableViewCell separatorInset设置为0分割线还是没有顶到头的问题

    只需要在自定义的Cell中添加以下代码即可 override func awakeFromNib() { super.awakeFromNib() layoutMargins = UIEdgeInse ...