hdu 4081 Qin Shi Huang's National Road System

来源:互联网 发布:java document类 编辑:程序博客网 时间:2024/06/06 04:13

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

题意:给出一些点的坐标代表一些城市并给出一个值代表这个城市的人口,总共有n-1条道路,其中有一条道路是魔法道路,这条道路没有权值,这条道路所连接的两城市的人口数和为A然后其余n-2条道路的权值之和为B,问A/B的最大值为多少

分析:牵扯到了所有边的权值的和,所以很容易想到最小生成树,但是这里有一个A不好确定。仔细分析过后,对于这条魔法道路,要么出现在最小生成树中,要么不在,加入出现在最小生成树中,我们直接去掉这个边的权值再一比就好了,假如不在呢?如果不在的话,首先A确定了,让A/B尽量大,那么让B小就好了,因为最小生成树已经保证了最小,所以我们只要把这个魔法道路加进去,然后把所形成回路中的最大权值边去掉不就好了?

综上,有两种操作:

1、魔法道路在最小生成树中:直接去掉边求

2、把边加进去然后去掉所形成回路中的最大边


难点:

求回路中的最大边(针对kruskal算法),我这里实现的是kruskal算法版本的,个人喜好这个算法,就用这个写的,prime算法可自动忽略这个难点,我这里使用一个dfs来解决的。

AC代码:

写的有点搓。。所以被注释掉的地方有点多,显得代码有点长。。。。

#include<iostream>#include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>using namespace std;#define fuck() cout<<"---------------"<<endlconst int maxed=1000+10;const int maxen=1000000+10;struct Node{    int u,v,val;}node[maxed];struct P{    int u,v;    double cost;    int val;}p[maxen];int n,ans,gen[maxed],h[maxed];bool vis[maxen],used[maxed];vector<int> ve[maxed];double d[maxed][maxed];int main(){    double dist(Node,Node);    void slove();    int N;    scanf("%d",&N);    while(N--){        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].val);            ve[i].clear();        }        ans=0;        for(int i=0;i<n-1;i++)            for(int j=i+1;j<n;j++){                p[ans].u=i;                p[ans].v=j;                p[ans].val=node[i].val+node[j].val;                p[ans].cost=dist(node[i],node[j]);                //cout<<"sdasad"<<" "<<p[ans].u<<" "<<p[ans].v<<" "<<p[ans].val<<" "<<p[ans].cost<<endl;                ans++;            }        slove();    }    return 0;}double dist(Node n1,Node n2){    return sqrt((n1.u-n2.u)*(n1.u-n2.u)+(n1.v-n2.v)*(n1.v-n2.v));}bool cmp(P p1,P p2){    return p1.cost<p2.cost;}void slove(){    void slove1();    int slove2(int);    void slove3(int,int);    void slove4(int,int,double);    slove1();    sort(p,p+ans,cmp);    fill(vis,vis+ans,false);    double res=0.0;    for(int i=0;i<ans;i++){        int x=slove2(p[i].u);        int y=slove2(p[i].v);        if(x!=y){            slove3(x,y);            res+=p[i].cost;            vis[i]=true;            //cout<<p[i].u<<" "<<p[i].v<<endl;            ve[p[i].u].push_back(p[i].v);            ve[p[i].v].push_back(p[i].u);        }    }    memset(d,0,sizeof(d));    for(int i=0;i<n;i++){        fill(used,used+n,false);        slove4(i,i,0.0);    }//    for(int i=0;i<n-1;i++){//        for(int j=i+1;j<n;j++)//            cout<<d[i][j]<<" ";//        cout<<endl;//    }    double max_=0.0;    //cout<<"            "<<res<<endl;    for(int i=0;i<ans;i++){        if(vis[i]){            max_=max(max_,p[i].val/(res-p[i].cost));            //cout<<max_<<" "<<p[i].val<<" "<<(res-p[i].cost)<<endl;        }        else{            max_=max(max_,p[i].val/(res-d[p[i].u][p[i].v]));            //cout<<max_<<" "<<p[i].val<<" "<<(res-d[p[i].u][p[i].v])<<endl;            //fuck();            //cout<<max_<<" "<<p[i].val<<" "<<d[p[i].u][p[i].v]<<endl;            //cout<<node[p[i].u].u<<" "<<node[p[i].u].v<<" "<<node[p[i].v].u<<" "<<node[p[i].v].v<<" "<<d[p[i].u][p[i].v]<<endl;        }        //cout<<max_<<" "<<p[i].val<<" "<<p[i].cost<<endl;    }    printf("%.2lf\n",max_);}void slove1(){    for(int i=0;i<n;i++){        gen[i]=i;        h[i]=0;    }}int slove2(int x){    if(x==gen[x])        return x;    return gen[x]=slove2(gen[x]);}void slove3(int x,int y){    if(x!=y){        if(h[x]<h[y])            gen[x]=y;        else{            if(h[x]==h[y])                h[x]++;            gen[y]=x;        }    }}void slove4(int now,int x,double _max){    //fuck();    int len=ve[x].size();    for(int i=0;i<len;i++){        int w=ve[x][i];        if(!used[w]){            used[w]=true;            d[now][w]=max(_max,dist(node[x],node[w]));            //d[ve[x][i]][now]=d[now][ve[x][i]];            slove4(now,w,d[now][w]);            //used[ve[x][i]]=false;        }    }}