hdu4081 Qin Shi Huang's National Road System

来源:互联网 发布:linux的vim命令 编辑:程序博客网 时间:2024/05/16 12:26

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2025 Accepted Submission(s): 739


Problem 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
241 1 201 2 30200 2 80200 1 10031 1 201 2 302 2 40

Sample Output
65.0070.00

Source
2011 Asia Beijing Regional Contest

Recommend
lcy
最小生成树的变形,也就是次小生成树的简单应用而矣,很经典啊!说说我对次小生成树的理解,首先我们知道 ,把最小生成树上,加一条边就能形成一定能生成一个环,把这个环的除了新加入的一边条外,去掉,环上的一个最大的值 ,那么得到的,不就是次小生成树了么?我们想想最后的答案,如果,加的那魔法边在最小生成树上,那么,就简单了,我们只需要总的最小生成树的值除去这条边的权值,就可以得到答案了,如果,加入的这条边不在最小生成树上,加入这条边后就会得到环,我们就要找到魔法边两个顶点之间最大的边权,去掉这条边之后,就一定能得到新的生成树,也就是次小生成树,那么,我们想,现在问题转化成了求在生成树上,两点间的,最大边权,我们用maxcost来存入,很明显,在求prime的时候,我们可以用dp的思想,先把它求出来,状态转移方程就是maxcot[pp][j]=max{maxcoxt[pre[ppp][j],lowc[j]},其中,lowc[j]表示从前先结点到此时结点的最小值 ,很明显,prime算法是n的平方,那么总的复杂度,在n的平方,对于这题而言,已经够 了!
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;#define MAXN 1005#define inf 2000000000double p[MAXN][3],lowc[MAXN],maxcost[MAXN][MAXN],dis[MAXN][MAXN];int use[MAXN][MAXN],visit[MAXN],pre[MAXN],n;double prime(){    double ans=0,maxx;int i,j,pp;    memset(visit,0,sizeof(visit));    memset(use,0,sizeof(use));    memset(pre,0,sizeof(pre));    memset(maxcost,0,sizeof(maxcost));    for(visit[1]=1,i=2;i<=n;i++)    lowc[i]=dis[1][i],pre[i]=1;    for(i=1;i<n;i++)    {        maxx=inf;pp=-1;        for(j=1;j<=n;j++)         if(!visit[j]&&lowc[j]<maxx)            {                maxx=lowc[j],pp=j;            }        use[pre[pp]][pp]=1,use[pp][pre[pp]]=1,visit[pp]=1;ans+=maxx;        for(j=1;j<=n;j++)        {            if(visit[j]&&j!=pp)maxcost[pp][j]=maxcost[j][pp]=max(maxcost[pre[pp]][j],lowc[pp]);            if(0==visit[j]&&lowc[j]>dis[pp][j])            lowc[j]=dis[pp][j],pre[j]=pp;        }    }    return ans;}int main(){    int tcase,i,j;double ans,re;    scanf("%d",&tcase);    while(tcase--)    {        scanf("%d",&n);        memset(p,0,sizeof(p));        memset(dis,0,sizeof(dis));        for(i=1;i<=n;i++)        {            scanf("%lf%lf%lf",&p[i][0],&p[i][1],&p[i][2]);            for(j=1;j<i;j++)                dis[i][j]=dis[j][i]=sqrt((p[i][0]-p[j][0])*(p[i][0]-p[j][0])+(p[i][1]-p[j][1])*(p[i][1]-p[j][1]));        }        re=prime();ans=-1;        for(i=1;i<=n;i++)        for(j=i+1;j<=n;j++)        {            if(!use[i][j])//最小生成树上,不包含这条边            ans=max(ans,(p[i][2]+p[j][2])/(re-maxcost[i][j]));            else            ans=max(ans,(p[i][2]+p[j][2])/(re-dis[i][j]));        }        printf("%.2f\n",ans);    }    return 0;}


原创粉丝点击