首页 > 代码库 > 图论trainning-part-1 H. Qin Shi Huang's National Road System
图论trainning-part-1 H. Qin Shi Huang's National Road System
H. Qin Shi Huang‘s National Road System
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d Java class name: MainDuring 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.
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.
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
241 1 201 2 30200 2 80200 1 10031 1 201 2 302 2 40
Sample Output
65.0070.00
解题:此题跟次小生成树没多大关系,只是涉及到最小生成树的遍历问题。解题思路就是先求出最小生成树,存储这棵树,然后再在这棵树上进行去边操作,此边的 两个端点城市的人口和 去除以 最小生成树的值减去此边的值 后的商,求这个商最大可能是多少。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 #define INF 0x3f3f3f11 using namespace std;12 const int maxv = 1010;13 const int maxe = 500*1010;14 struct arc{15 int u,v;16 double w;17 };18 int uf[maxv];19 vector<int>g[maxv];20 arc e[maxe],tree[maxe];21 int n,px[maxv],py[maxv],val[maxv];22 int maxVal,es,ads;23 double Minst;24 bool vis[maxv];25 bool cmp(const arc &x,const arc &y){26 return x.w < y.w;27 }28 void dfs(int u){29 vis[u] = true;30 if(val[u] > maxVal) maxVal = val[u];31 for(int i = 0; i < g[u].size(); i++){32 if(!vis[g[u][i]]) dfs(g[u][i]);33 }34 }35 int findF(int x){36 if(x != uf[x])37 uf[x] = findF(uf[x]);38 return uf[x];39 }40 double dis(int i,int j){41 double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]);42 return sqrt(temp);43 }44 void kruskal(){45 for(int i = 0; i < es; i++){46 int x = findF(e[i].u);47 int y = findF(e[i].v);48 if(x != y){49 Minst += e[i].w;50 uf[x] = y;51 g[e[i].u].push_back(e[i].v);52 g[e[i].v].push_back(e[i].u);53 tree[ads++] = e[i];54 }55 }56 }57 int main(){58 int t,i,j;59 scanf("%d",&t);60 while(t--){61 scanf("%d",&n);62 for(i = 1; i <= n; i++){63 scanf("%d%d%d",px+i,py+i,val+i);64 g[i].clear();65 uf[i] = i;66 }67 ads = es = 0;68 Minst = 0;69 for(i = 1; i <= n; i++)70 for(j = i+1; j <= n; j++){71 e[es++] = (arc){i,j,dis(i,j)};72 }73 sort(e,e+es,cmp);74 kruskal();75 double ans = 0,temp;76 for(i = 0; i < ads; i++){77 int u = tree[i].u;78 int v = tree[i].v;79 memset(vis,false,sizeof(vis));80 vis[v] = true;81 temp = maxVal = 0;82 dfs(u);83 temp += maxVal;84 memset(vis,false,sizeof(vis));85 maxVal = 0;86 vis[u] = true;87 dfs(v);88 temp += maxVal;89 ans = max(ans,temp/(Minst-tree[i].w));90 }91 printf("%.2f\n",ans);92 }93 return 0;94 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。