首页 > 代码库 > 最小生成树 次小生成树 hdu4081

最小生成树 次小生成树 hdu4081

A - Qin Shi Huang‘s National Road System
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 
 
应用时:2h,如果第二次看到这个题就应当用45min-1h30min
实际用时:1days19h
经验教训:不要被表面吓住了..其实就是那么简单的东西
思路:
我们看到这个就想到最小生成树,那么首先假设T是最小生成树,权值和为allt,这里就有一非常需要熟用的最小生成树性质,即:
对于任意d[i][j]为题目所给的ij距离,有两种情况
1 d[i][j]在最小生成树内
2 i,j由最小生成树上的边 (i,x1),(x1,x2)...(xk,j)组成,那么每个中间路径上的边都有d[xk][xk+1]<d[i][j]要不然d[i][j]就在里面了
那么现在我们假设取的就是i,j边,那么就相当于是添加了一条边,d[i][j]=0,
1 当d[i][j]在最小生成树内的时候,相当于直接更新了最小生成树,allt-d[i][j]即可,所有中间路径为d[i][j]的仍然是最小生成树
2 当d[i][j]不在最小生成树内的时候,这时添加d[i][j]=0形成了环,(其实第一种情况也是环,)这时多连了一条边,于是我们想到要把最大的那条边取出来,取出来肯定是树,不在以i为根的子树上的点不需要变化,对于以i为根的子树,之前不在里面的边现在还是不在里面,不然的话子树的权值反而在增大,那么对于这个环肯定是删掉最大边形成最小子树
那么如何求出最小生成树上从i到j的路径所必须经过的最大边呢?用个dp就行了
dp[i][j]=max(dp[i][i的前驱节点],dp[i][j]);//假设i先加入最小生成树
 
这道题还有各种姿势:这个像次小生成树,还有枚举各边,删除这条边的话在两边的树上取人口最大的两个城市,或者kruscal()直接求最大边,不过不想写了
#include <cstdio>#include <cstring>#include  <cmath>#include <algorithm>#include <queue>using namespace std;const int maxn=1105;int n;struct P{    int x,y,p;}v[maxn];//记录城市double d[maxn][maxn];//城市之间的给定距离bool vis[maxn];//记录prim中间过程double maxd[maxn][maxn];//用于记录在树上从i到j需要经过的最大边typedef pair<int ,int > point;//记录从second到first的一条边typedef pair<double,point> pr;//first 距离 second 起终点priority_queue<pr,vector<pr>,greater<pr> >que;//用来prim取最小权值边double prim(){    memset(vis,0,sizeof(vis));    vis[0]=true;//起点定为0已经在树上了    int num=1;//当前最小生成树中的节点数    while(!que.empty())que.pop();//多组case    double ans=0;//记录总边权    for(int i=1;i<n;i++){        que.push(pr(d[0][i],point(i,0)));//把与0相连的加入prim的优先队列    }    while(num<n){        double td=que.top().first;//取信息        int  t=que.top().second.first;        int f=que.top().second.second;        que.pop();      if(vis[t])continue;//如果已经加入了那么就不用管了        vis[t]=true;num++;ans+=td;//加点加边        maxd[t][f]=maxd[f][t]=td;//dp求最大边的起点,这两点直接相连        for(int i=0;i<n;i++){            if(!vis[i]){                que.push(pr(d[t][i],point(i,t)));//不在里面的继续更新            }            else {                if(i!=t){                    maxd[t][i]=maxd[i][t]=max(maxd[f][i],td);//在里面的dp求值                }            }        }    }    return ans;}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].p);        }        for(int i=0;i<n;i++){//建图            for(int j=0;j<=i;j++){                d[i][j]=d[j][i]=sqrt((v[i].x-v[j].x)*(v[i].x-v[j].x)+(v[i].y-v[j].y)*(v[i].y-v[j].y));            }        }        double allt=prim();        double maxrate=-1;        for(int i=0;i<n;i++){//取所有可能的城市对            for(int j=0;j<i;j++){                double rate=(v[i].p+v[j].p)/(allt-maxd[i][j]);                maxrate=max(maxrate,rate);            }        }        printf("%.2f\n",maxrate);    }    return 0;}