图论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 ...
随机推荐
- javaScript中的闭包原理 (译)
这篇文章通过javaScript代码解释了闭包的原理,来让编程人员理解闭包.它不是写给大牛或使用功能性语言进行编程的程序员的.一旦意会了其核心概念,闭包理解起来并不难.然而,你不可能通过阅读任何有关闭 ...
- ubuntu键盘映射
在sublime下开发习惯把CapsLock和Shift间交换,windows下有很多软件可以修改键盘映射,在ubuntu下可以是哦用xmodmap命令,使用方法如下: 在自己用户的home目录下新建 ...
- Git .gitignore 设置为全局global
在操作Git时,我们会将修改的内容$git add . 到Git,Git会提示我们哪些文件都修改了.此时提示中会包括系统自动修改的文件,bin文件等.而我们add到Git时,并不希望将这些文件也一同a ...
- mysqldumpslow及explain使用简介
- SQL server 数据库基础语句 子查询 基础函数
上一章 说了下 子查询的意义是 把一条查询语句当做值来使用 select *from car //查询汽车的信息 假设我知道一个汽车的编号是 c021 但是我要查询 比这个汽车价格高的汽车信息 ...
- ftpclient 遇到的一些问题
1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...
- python小括号( )与中括号 [ ]
在python中小括号()表示的是tuple元组数据类型,元组是一种不可变序列. >>> a = (1,2,3) >>> a (1, 2, 3) >>& ...
- LeetCode || 递归 / 回溯
呜呜呜 递归好不想写qwq 求“所有情况”这种就递归 17. Letter Combinations of a Phone Number 题意:在九宫格上按数字,输出所有可能的字母组合 Input: ...
- 【转载】WPF DataGrid 性能加载大数据
作者:过客非归 来源:CSDN 原文:https://blog.csdn.net/u010265681/article/details/76651725 WPF(Windows Presentatio ...
- js正则函数match、exec、test、search、replace、split使用集合
match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...