H. Qin Shi Huang's National Road System

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty ---- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:
There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang.
Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible ---- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.
Would you help Qin Shi Huang?
A city can be considered as a point, and a road can be considered as a line segment connecting two points.

 

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 解题:此题跟次小生成树没多大关系,只是涉及到最小生成树的遍历问题。解题思路就是先求出最小生成树,存储这棵树,然后再在这棵树上进行去边操作,此时的两颗树上人口数最多的两个城市人口数的和 去除以 最小生成树的值减去此边的值 后的商,求这个商最大可能是多少。 Kruskal写法
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxv = ;
const int maxe = *;
struct arc{
int u,v;
double w;
};
int uf[maxv];
vector<int>g[maxv];
arc e[maxe],tree[maxe];
int n,px[maxv],py[maxv],val[maxv];
int maxVal,es,ads;
double Minst;
bool vis[maxv];
bool cmp(const arc &x,const arc &y){
return x.w < y.w;
}
void dfs(int u){
vis[u] = true;
if(val[u] > maxVal) maxVal = val[u];
for(int i = ; i < g[u].size(); i++){
if(!vis[g[u][i]]) dfs(g[u][i]);
}
}
int findF(int x){
if(x != uf[x])
uf[x] = findF(uf[x]);
return uf[x];
}
double dis(int i,int j){
double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]);
return sqrt(temp);
}
void kruskal(){
for(int i = ; i < es; i++){
int x = findF(e[i].u);
int y = findF(e[i].v);
if(x != y){
Minst += e[i].w;
uf[x] = y;
g[e[i].u].push_back(e[i].v);
g[e[i].v].push_back(e[i].u);
tree[ads++] = e[i];
}
}
}
int main(){
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i <= n; i++){
scanf("%d%d%d",px+i,py+i,val+i);
g[i].clear();
uf[i] = i;
}
ads = es = ;
Minst = ;
for(i = ; i <= n; i++)
for(j = i+; j <= n; j++){
e[es++] = (arc){i,j,dis(i,j)};
}
sort(e,e+es,cmp);
kruskal();
double ans = ,temp;
for(i = ; i < ads; i++){
int u = tree[i].u;
int v = tree[i].v;
memset(vis,false,sizeof(vis));
vis[v] = true;
temp = maxVal = ;
dfs(u);
temp += maxVal;
memset(vis,false,sizeof(vis));
maxVal = ;
vis[u] = true;
dfs(v);
temp += maxVal;
ans = max(ans,temp/(Minst-tree[i].w));
}
printf("%.2f\n",ans);
}
return ;
}

Prim写法:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxv = ;
const int maxe = *;
struct arc{
int u,v;
double w;
}e[maxe];
int px[maxv],py[maxv],val[maxv],pre[maxv];
double d[maxv],Minst,mp[maxv][maxv];
int n,tot,mxp;
vector<int>g[maxv];
bool vis[maxv];
double dis(int i,int j){
double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]);
return sqrt(temp);
}
void prim(){
int i,j,index;
double theMin;
for(i = ; i <= n; i++){
d[i] = mp[][i];
pre[i] = ;
}
for(i = ; i < n; i++){
theMin = INF;
for(j = ; j <= n; j++){
if(d[j] > && d[j] < theMin) theMin = d[index = j];
}
e[tot++] = (arc){pre[index],index,theMin};
g[index].push_back(pre[index]);
g[pre[index]].push_back(index);
Minst += theMin;
d[index] = -;
for(j = ; j <= n; j++)
if(d[j] > && d[j] > mp[index][j]){
d[j] = mp[index][j];
pre[j] = index;
}
}
}
void dfs(int u){
vis[u] = true;
if(val[u] > mxp) mxp = val[u];
for(int i = ; i < g[u].size(); i++){
if(!vis[g[u][i]]) dfs(g[u][i]);
}
}
int main(){
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i <= n; i++){
scanf("%d%d%d",px+i,py+i,val+i);
g[i].clear();
}
for(i = ; i <= n; i++){
for(j = i+; j <= n; j++)
mp[i][j] = mp[j][i] = dis(i,j);
}
Minst = ;
tot = ;
prim();
double ans = ,temp;
for(i = ; i < tot; i++){
int u = e[i].u;
int v = e[i].v;
memset(vis,false,sizeof(vis));
temp = ;
mxp = ;
vis[v] = true;
dfs(u);
temp += mxp;
memset(vis,false,sizeof(vis));
vis[u] = true;
mxp = ;
dfs(v);
temp += mxp;
ans = max(ans,temp/(Minst-e[i].w));
}
printf("%.2f\n",ans);
}
return ;
}

图论trainning-part-1 H. Qin Shi Huang's National Road System的更多相关文章

  1. [hdu P4081] Qin Shi Huang’s National Road System

    [hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

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

  3. hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)

    题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: ...

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

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

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

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

  8. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

    Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...

  9. HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏

    Qin Shi Huang's National Road System                                                                 ...

随机推荐

  1. CentOS远程监控

    近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...

  2. okhttputils使用(zhuan)

    OkHttpUtils 封装了okhttp的网络框架,支持大文件上传下载,上传进度回调,下载进度回调,表单上传(多文件和多参数一起上传),链式调用,可以自定义返回对象,支持Https和自签名证书,支持 ...

  3. 【Web应用-网络连接】关于 Azure Web 应用 4 分钟空闲连接的限制

    Azure Web 应用后台在处理耗时较长的请求时,并且在此期间,客户端和 Azure Web 应用没有数据交互,即 TCP 连接一直处于空闲状态,此种情况超过 4 分钟后,Azure Web 应用会 ...

  4. OPENFIRE 接收数据流程图

    此图网上已经有,怎奈我不能上传大于10M的图片,所以截图了!各位请脑补!

  5. COGS 788. 昵称

    788. 昵称 ★☆   输入文件:nickname.in   输出文件:nickname.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] ZSUQ送信者与腾讯QQ相似 ...

  6. lua_to_luac

    #!/bin/sh `rm -rf allLua.zip` `mkdir ./tempScripts` `mkdir ./tempScripts/scripts` `cp -a ./scripts/ ...

  7. java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal问题解决

    使用Maven构建项目并加载spring配置文件时,报如下异常 Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTrav ...

  8. spark简单入门

    本文由cmd markdown编辑,原始链接:https://www.zybuluo.com/jewes/note/35032 RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spa ...

  9. 用dfs遍历联通块(优化)

    一.题目(CF 598D) 输入一个n x m的字符矩阵,求从某个空点出发,能碰到多少面墙壁,总共询问k次.(3 ≤m,n ≤1000,1 ≤ k ≤ min(nm,100 000)) 二.解题思路 ...

  10. SNP|RELP|genetic polymorphism|

    5.3个体基因组呈现广泛变化 遗传多态性:一个基因座上存在多个等位基因(因为野生型不止一种基因)的现象,但是只有这多种等位基因满足:1.多个基因稳定存在2.基因在种群中数目大于1%时,认为该基因座多态 ...