分析: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. Netty多线程处理机制

    技术点描述 本文主要研究NioServerSocketChannelFactory类和NioDatagramChannelFactory类, 以及这两个类的各自作用. 由于基于pipelineFact ...

  2. I/O 流---输出流

    输出流(写入数据) a. 字节输出流 OutputStram 输出流的父类 FileOutputStream:  继承OutputStream 方法: OutputStream os=new File ...

  3. ExtJS4.2学习(三)Grid表格(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-07/172.html --------------- ...

  4. BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告

    这个题感觉比较简单,但却比较容易想残.. 我不会用树状数组求这个原排列,于是我只好用线段树...毕竟 Gromah 果弱马. 我们可以直接依次求出原排列的元素,每次找到最小并且最靠右的那个元素,假设这 ...

  5. PAT-乙级-1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  6. 8 行 Node.js 代码实现代理服务器

    接触 Node.js 已有多年,一直喜欢它的单线程模型和异步IO特性,以及 JavaScript 语言本身的灵活性.同时,JavaScript 前后端通吃,在全栈开发领域具有独特的优势.今天就来看看作 ...

  7. How To Monitor Remote Linux Host using Nagios 3.0

    In the previous post Nagios 3.0 Jumpstart guide , I explained the overview, installation and configu ...

  8. socket选项自带的TCP异常断开检测

    TCP异常断开是指在突然断电,直接拔网线等等情况下,如果通信双方没有进行数据发送通信等处理的时候,无法获知连接已经断开的情况. 在通常的情况下,为了使得socket通信不受操作系统的限制,需要自己在应 ...

  9. 宜昌IT软件资源汇总

    三峡云计算中心大厦 http://castd-ycci.com/ 三峡云计算中心http://www.sanxiacloud.com/index.htm 宜昌创业者(罗新:小卡片做成大产业 年销售额5 ...

  10. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...