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代表所建的魔法路的连接的城市的市民的人数 ...
随机推荐
- bnuoj 1053 EASY Problem (计算几何)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1053 [题意]:基本上就是求直线与圆的交点坐标 [题解]:这种题我都比较喜欢用二分,三分做,果然可以 ...
- PAT-乙级-1006. 换个格式输出整数 (15)
1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...
- FaceNet--Google的人脸识别(转)
引入 随着深度学习的出现,CV领域突破很多,甚至掀起了一股CV界的创业浪潮,当次风口浪尖之时,Google岂能缺席.特贡献出FaceNet再次刷新LFW上人脸验证的效果记录. 本文是阅读FaceNet ...
- PHP session有效期session.gc_maxlifetime的设置方法
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适 ...
- PHP 怎么随机获取数组里面的值
注意array_rand随机返回的是KEY值的集合 <?php srand((float) microtime() * 10000000); $input = array("Neo&q ...
- ECNU1104-BFS
简单的BFS题意:多起点多终点 /* 简单的BFS 题意:多起点多终点 */ #include<stdio.h> #include<string.h> #include< ...
- codeforces #305 A Mike and Frog
挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...
- PHP基础语法2
数组 PHP有两种数组:索引数组.关联数组. 函数 自定义函数 自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰 返回值 使用return关键字可以使函数返回值,可以返回包括数 ...
- [codility]Min-abs-sum
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...
- Android:控件GridView的使用
如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView. <?xml version="1.0" encoding="u ...