hdu4081 Qin Shi Huang's National Road System(次小生成树)

来源:互联网 发布:知乎类似洛丽塔小说 编辑:程序博客网 时间:2024/06/05 04:17

Qin Shi Huang’s National Road System

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


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
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个城市修n-1路,徐福说,我可以随便搞一条路出来,不要钱。
秦始皇说,要A/B最大,A代表修路的人力(路两端的人口数),B代表的是其他n-2条路的总长。

次小生成树模版。
首先求出原图最小生成树,记录权值之和为MinST。枚举添加每条不在最小生成树上的边(u,v)
加上以后一定会形成一个环。找到环上权值第二大的边(即除了(u,v)以外的权值最大的边),把它删掉,
计算当前生成树的权值之和。取所有枚举修改的生成树权值之和的最小值,就是次小生成树

#include<iostream>using namespace std;#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stdlib.h>#include<vector>#include<queue>#include<deque>#include<map>#include<set>#include<time.h>#define si(x) scanf("%d",&(x))#define s2(x,y) scanf("%d%d",&(x),&(y))#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))#define sl(x) scanf("%lld",&(x))#define read int Tcase;scanf("%d",&Tcase);while(Tcase--)#define cls(x,y) memset((x),(y),sizeof((x)));#define cls0(x) memset((x),0,sizeof((x)));#define cls_1(x) memset((x),-1,sizeof((x)));//#define max(value_a,value_b,value_c) max(max(value_a,value_b),value_c)#define GT(x) (x)=clock();#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))typedef long long LL;typedef unsigned long long ULL;const int maxint=((~((unsigned)(0)))>>1);const LL maxll=((~((unsigned long long)(0)))>>1);const double PI=acos(-1.0);const double inf=1e12;const int maxn=1005;int n;struct note {    int x,y,p;} b[maxn];double a[maxn][maxn];double dp[maxn][maxn];int pre[maxn];bool used[maxn];int peo;bool mst[maxn][maxn];double dis[maxn];double CALC(int i,int j) {    int x11=b[i].x;    int y11=b[i].y;    int x21=b[j].x;    int y21=b[j].y;    return sqrt((y11-y21)*1.0*(y11-y21)*1.0+(x11-x21)*1.0*(x11-x21));}void PRIM() {    cls0(dp);    cls(used,false)    cls(mst,false)    double minn;    int pos,te;    for(int i=1; i<=n; ++i) {        pre[i]=1;        dis[i]=a[i][1];    }    used[1]=true;    double ansT=0;    for(int i=1; i<n; ++i) {        pos=-1,minn=inf;        //算法的瓶颈在于维护两点之间的最大边权        //所以没有必要在此使用优先队列寻找最小的距离        for(int j=1; j<=n; ++j) {            if(!used[j]&&dis[j]<minn) {                minn=dis[j],pos=j;            }        }        if(pos==-1)break;        used[pos]=true;        ansT+=minn;        te=pre[pos];        mst[pos][te]=mst[te][pos]=true;//在最小生成树中        dp[te][pos]=dp[pos][te]=a[pos][te];        for(int j=1; j<=n; ++j) {            if(used[j]&&(j^pos)) {                dp[j][pos]=dp[pos][j]=max(dp[te][j],dp[te][pos]);            }        }        for(int j=1; j<=n; ++j) {            if((!used[j])&&(dis[j]>a[pos][j])) {                dis[j]=a[pos][j];                pre[j]=pos;            }        }    }    double ans=0;    for(int i=1; i<=n; ++i) {        for(int j=i+1; j<=n; ++j) {            int ps=b[i].p+b[j].p;            if((mst[i][j])) {                ans=max(ans,(ps*1.0/(ansT-a[i][j])));            } else {                ans=max(ans,(ps*1.0/(ansT-dp[i][j])));            }        }    }    printf("%.2lf\n",ans);}int main() {#ifdef tangge    clock_t tSTART,tEND,t3;    GT(tSTART);    freopen("4081.txt","r",stdin);#endif // tangge    read {        si(n);        for(int i=1; i<=n; ++i) s3(b[i].x,b[i].y,b[i].p);        cls(a,0)        for(int i=1; i<=n; ++i) {            for(int j=i+1; j<=n; ++j) {                a[i][j]=a[j][i]=CALC(i,j);            }        }        PRIM();    }#ifdef tangge    GT(tEND);    printf("%.8lf\n",(tEND-tSTART)/1000.0);#endif // tangge    return 0;}
1 0
原创粉丝点击