图论trainning-part-1 H. Qin Shi Huang's National Road System
H. Qin Shi Huang's National Road System
64-bit integer IO format: %I64d Java class name: Main

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
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
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的更多相关文章
- [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 ...
- 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(最小生成树+bfs)
题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory 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 ...
- 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 ...
- 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 次小生成树变种
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
随机推荐
- React 实践记录 01 组件开发入门
Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程序员创建,是一个非常强大的 ...
- Java设计模式之单例模式 - Singleton
用来创建独一无二的,是能有一个实例的对象的入场券.告诉你一个好消息,单例模式的类图可以说是所有模式的类图中最简单的,事实上,它的类图上只有一个类!但是,可不要兴奋过头,尽管从类设计的视角来说很简单,但 ...
- 提升 Web开发性能的 10 个技巧
随着网络的高速发展,网络性能的持续提高成为能否在芸芸App中脱颖而出的关键.高度联结的世界意味着用户对网络体验提出了更严苛的要求.假如你的网站不能做到快速响应,又或你的App存在延迟,用户很快就会移情 ...
- bufferedinputStream操作
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- NF!=1
NF表示列数,不等于1表示列数不为1列
- mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
解决方案如下:
- 机器学习之-奇异值分解(SVD)原理详解及推导
转载 http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有需要补充 ...
- 爬虫_python3_requests_2
pip install requests 进行简单的操作 发送一个get请求 # 发送请求 import requests response = requests.get('http://httpbi ...
- uaf-湖湘杯2016game_学习
0x00 分析程序 根据分析,我们可以得到以下重要数据结构 0x01 发现漏洞 1.在武器使用次数耗光后,程序会把存储该武器的堆块free,在free的时候没有清空指针,造成悬挂指针 2.commen ...
- JS添加验证页面中script标签中是否存在jquery文件
window.onload = function() { var al = document.getElementsByTagName("script"); var new_ele ...