图论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 ...
随机推荐
- 代码中看见一共8个变量参数{SEO,0,0,0,0,0,0,0} 解读!{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}
代码中看见{SEO,0,0,0,0,0,0,0}{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}解读! 举个例子: {GetNew,977,0,23,500,0,0 ...
- jQuery对表格的操作
1.表格变色 (1)普通的隔行变色 CSS代码: .even{background:#fff;} //偶数行样式 .even{background:#fff;} //奇数行样式 ①包括表头 $ ...
- VBA小记
要放假了,可是我们,我还是煎熬! 最让人不爽的是媳妇也需要加班加点的完成一些看起来很EASY的事: 统计数据,把几个表合并…… EXCEL本人还是懂得一点点的(我不想说我是学计算机的,我怕给学计算机的 ...
- MyBatis的数据库操作
MyBatis的数据库操作 大学毕业有一段时间了,作为一名没有任何开发工作经验的大专毕业生想找到一份软件开发的工作确实很难,但我运气还算不错,应聘上一份java web开发的工作.作为一名新人,老板给 ...
- JS常用的技术
思考与总结 1.模块化 曾看到某大牛说:模块化和组件化是前端开发的一大趋势.所谓的模块化一般是指为了实现一个特定的功能而将所有的代码(对象)封装成一个模块.而AMD就是requireJS为指定模块规范 ...
- LookAround开元之旅(持续更新中...)
应用介绍随便瞧瞧是一款为android用户量身定做的免费图文资讯软件集美食,文学,语录等频道于一体界面简洁,操作流畅,图文分享,个性收藏是广大卓粉的必备神器APK下载 -->https://ra ...
- 洛谷 P1376 机器工厂
题目描述 小T开办了一家机器工厂,在N(N<=10000)个星期内,原材料成本和劳动力价格不断起伏,第i周生产一台机器需要花费Ci(1<=Ci<=5000)元.若没把机器卖出去,每保 ...
- Codeforces Round #316 (Div. 2) C Replacement 扫描法
先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++. ...
- 传统BP对比CNN
传统BP vs CNN 存在2个问题 传统BP网络存在的问题: 权值太多,计算量太大 权值太多,需要大量样本进行训练 传统的BP来处理图像问题的话因为计算权值太多太大. 网络的建立要根据数据的大小来建 ...
- C04 模块化开发
目录 模块化开发概述 函数概述 如何使用函数 字符串处理函数 模块化开发特点 模块化开发概述 概述 C语言是面向过程的语言,意味着编写C语言程序的时候,我们要像计算机一样思考如何设计程序. 模块化开发 ...