Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10567 Accepted Submission(s): 3727
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081
Description:
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
题意:
给出一个无向图以及n个点的坐标以及点对应的权值。现在选出一条道路,使得修建它的费用为0,问A/B的最大值是多少,其中A为选出道路的两个端点的权值和,B为将图连通其它道路的花费。
题解:
想法就是枚举每条道路然后来计算,但是每次都求一次最小生成树有点麻烦,可以这样考虑:
如果这条道路在原图最小生成树中,那么答案就是(d[u]+d[v]) / (sum-dis[u][v]);
如果不在最小生成树中,那么答案也是(d[u]+d[v]) / (sum-dis[u][v]),我们加入这条边后,图必定会形成一个环,那么我们应该去掉最小生成树中u到v路径上的最大边权值。
那么,上面两个式子的dis含义都为点u与点v之间的最大边权值,关键把这个算出来就行了。
计算的话dfs一次就行了,O(n^2)就可以完成,只需要枚举已经算出来的点来进行更新。其实这就是求最小瓶颈路。
具体代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #include <cmath> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 1005; struct node{ int x,y; }p[N]; int t,n,tot; int a[N]; double dis(int x,int y){ return sqrt((p[x].x-p[y].x)*(p[x].x-p[y].x)+(p[x].y-p[y].y)*(p[x].y-p[y].y)); } struct Edge{ int u,v;double w; bool operator < (const Edge &A)const{ return w<A.w; } }e[N*N]; int f[N],mp[N][N]; int find(int x){ return f[x]==x?f[x]:f[x]=find(f[x]); } double Kruskal(){ double ans=0; for(int i=0;i<=n+1;i++) f[i]=i; for(int i=1;i<=tot;i++){ int u=e[i].u,v=e[i].v; int fx=find(u),fy=find(v); if(fx==fy) continue ; f[fx]=fy; mp[u][v]=mp[v][u]=1; ans+=e[i].w; } return ans ; } double d[N][N]; int check[N]; void dfs(int u,int fa){ for(int i=1;i<=n;i++){ if(check[i]) d[i][u]=d[u][i]=max(d[i][fa],dis(fa,u)); } check[u]=1; for(int i=1;i<=n;i++){ if(mp[i][u] && i!=fa) dfs(i,u); } } int main(){ cin>>t; while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++){ int x,y; scanf("%d%d%d",&x,&y,&a[i]); p[i]=node{x,y}; } tot = 0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) e[++tot]=Edge{i,j,dis(i,j)}; memset(mp,0,sizeof(mp)); sort(e+1,e+tot+1); double sum=Kruskal(); memset(d,0,sizeof(d)); memset(check,0,sizeof(check)); dfs(1,-1); double ans = 0; for(int i=1;i<=tot;i++){ int u=e[i].u,v=e[i].v; double w=e[i].w; ans=max(ans,(a[u]+a[v])/(sum-d[u][v])); } printf("%.2lf ",ans); } return 0; }