HDU 4081 Qin Shi Huang's National Road System 最小生成树
分析: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 最小生成树的更多相关文章
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
- 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 [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
随机推荐
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- 使用Pod集成Bugtags填坑记
最近某朋友的朋友的创业公司新出了一个工具叫Bugtags,说是可以让APP测试变得so easy,于是动手来做1.1.0的版本集成,先把WEB首页贴在下面,感兴趣的同学可以去look一下:https: ...
- Unity3d + NGUI 的多分辨率适配(黑边)
原地址:http://www.2cto.com/kf/201310/250921.html 一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS设备的分辨率主 ...
- IsBadReadPtr|IsBadWritePtr调试崩溃
遇到一未找到必然出现条件的崩溃,不知道什么时候能触发崩溃,崩溃dump显示,试图访问了非法的内存或者写入了非法的内存 此时如下两个函数就比较有用了: BOOL WINAPI IsBadReadPtr( ...
- HDU2110+母函数
/* 母函数(生成函数) 题意: 有n种资产,每种资产num份,每份有val的价值 问取出总价值的1/3有多少种方案 */ #include<stdio.h> #include<st ...
- sql快捷键
请柬:https://msdn.microsoft.com/zh-cn/library/ms174205.aspx
- [模拟]ZOJ3480 Duck Typing
题意:给了一坨...按题目意思输出就好了... 给一组案例 begin class d class c:d class b:c class a:b def d.m def d.n call a.m e ...
- Eclipse字体修改设置
修改字体 Window -> Preferences -> General -> Appearences -> Colors and Fonts 选择java选项,看到Java ...
- SGU 168
SGU 168,寻找矩阵中右上方,右方,下方最小的元素,采用动态规划解答. #include <iostream> #include <vector> #include < ...
- linux 新建用户、用户组 以及为新用户分配权限
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号 一方面可以帮助系统管理员对使用系统的用户进 ...