分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html

这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成树然后查询就好了

然后预处理那个数组是O(n^2)的,这样总时间复杂度是O(n^2+m)

这是因为这个题n比较小,如果n大的时候,就需要路径查询了,比如LCA 或者树链剖分达到O(mlogn)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int N=;
double x[N],y[N],d[N][N],a[N];
int fa[N],head[N],tot,cnt,T,n;
struct Edge{
int v,next;
double w;
}edge[N<<];
void add(int u,int v,double w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
struct Node{
int u,v;
bool mark;
double w;
bool operator<(const Node &rhs)const{
return w<rhs.w;
}
}p[N*N];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int s;
void dfs(int u,int f,double t){
d[s][u]=t;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f)continue;
dfs(v,u,max(t,edge[i].w));
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d",&n);
cnt=tot=;
memset(d,,sizeof(d));
for(int i=;i<=n;++i){
fa[i]=i,head[i]=-;
scanf("%lf%lf%lf",&x[i],&y[i],&a[i]);
for(int j=;j<i;++j){
++cnt;
p[cnt].u=i,p[cnt].v=j;p[cnt].mark=;
p[cnt].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
}
sort(p+,p++cnt);
int tmp=n;
double mst=,ans=;
for(int i=;i<=cnt;++i){
int u=find(p[i].u),v=find(p[i].v);
if(u!=v){
--tmp;
fa[u]=v;
mst+=p[i].w;
p[i].mark=;
add(p[i].u,p[i].v,p[i].w);
add(p[i].v,p[i].u,p[i].w);
if(tmp==)break;
}
}
for(int i=;i<=n;++i){
s=i;dfs(i,,);
}
for(int i=;i<=cnt;++i){
double aa=a[p[i].u]+a[p[i].v],bb;
if(p[i].mark)bb=mst-p[i].w;
else bb=mst-d[p[i].u][p[i].v];
ans=max(ans,aa/bb);
}
printf("%.2f\n",ans);
}
return ;
}

HDU 4081 Qin Shi Huang's National Road System 最小生成树的更多相关文章

  1. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

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

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

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

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

  6. hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  7. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

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

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

  9. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

随机推荐

  1. 1187: [HNOI2007]神奇游乐园 - BZOJ

    Description 经历了一段艰辛的旅程后,主人公小P乘坐飞艇返回.在返回的途中,小P发现在漫无边际的沙漠中,有一块狭长的绿地特别显眼.往下仔细一看,才发现这是一个游乐场,专为旅途中疲惫的人设计. ...

  2. 1202: [HNOI2005]狡猾的商人 - BZOJ

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  3. Deadline来了,如何按时结题?

  4. hdu 4577 X-Boxes 大数

    java水过…… 代码如下: import java.math.*; import java.util.*; public class Main { public static void main(S ...

  5. IText 中文字体解决方案 生成doc文档

    IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...

  6. Java将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)

    import org.junit.Test; import java.io.*; /** * User: HYY * Date: 13-8-18 * Time: 下午8:11 * To change ...

  7. intelliJ idea读取资源文件

    原文:intelliJ idea读取资源文件 原文地址 http://yanwushu.sinaapp.com/intellij-idea_raed_resource_file/ 官方文档 以下是je ...

  8. P134、面试题22:栈的压入、弹出序列

    题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4.5是某栈的压栈序列,序列4,5,3,2,1是该压栈序列对 ...

  9. hadoop2.2.0安装

    64位编译和安装 http://blog.csdn.net/licongcong_0224/article/details/12972889 http://blog.csdn.net/w1377026 ...

  10. 解决git Push时请求username和password,而不是ssh-key验证

    转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...