uva1494 - Qin Shi Huang's National Road System 次小生成树

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 roadsystem. There is a story about the road system:

There were n cities in China and Qin Shi Huang wanted them all be connected byn - 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 roadsystem may not cost too many people's life. A daoshi(some kind of monk) named Xu Fu told Qin Shi Huangthat he could build a road by magic and that magic roadwould cost no money and no labor. But Xu Fu couldonly build ONE magic road for Qin Shi Huang. So QinShi 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 ofA/B (theratio of A toB) must be the maximum, which A is the total population of the two cites connected by the magic road, andB 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 aret test cases (t10).

For each test case:

The first line is an integer n meaning that there aren cities (2 < n1000).

Then n lines follow. Each line contains three integersX, Y and P (0X,Y1000, 0 <P < 100000). (X, Y) is the coordinate of a city andP 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个城市,要建一些路。道士能帮忙用法术修一条,不花钱。设道士修的路连接的城市人口为A,其它路总长为B。找出A/B最大的方案。

  次小生成树是最小生成树加一条边u-v后再删除u-v路径上权值最大的一条边,因为最小生成树加边后会形成环,所以在环中删掉一条各个点还是可达。同理这个题就只用枚举道士修的u-v,求出最小生成树后删除u-v路径上最大权值的一条边。求maxcost[u][v]复杂度O(N^2),把无根的最小生成树转成有根树,在这个过程中求出maxcost。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<cmath>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define eps 1e-3
using namespace std;
typedef long long LL;
int T,N,M,pa[MAXN],x[MAXN],y[MAXN],p[MAXN];
vector<int> G[MAXN],node;
vector<double> C[MAXN];
double maxcost[MAXN][MAXN];
int find(int x){
    return x==pa[x]?x:pa[x]=find(pa[x]);
}
struct Edge{
    int x,y;
    double dist;
    bool operator < (const Edge& rhs) const{
        return dist<rhs.dist;
    }
}e[MAXN*MAXN];
double MST(){
    M=0;
    for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++) e[M++]=(Edge){i,j,sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))};
    sort(e,e+M);
    for(int i=0;i<N;i++){
        pa[i]=i;
        G[i].clear();
        C[i].clear();
    }
    double ret=0;
    int cnt=0;
    for(int i=0;i<M;i++){
        int x=e[i].x,y=e[i].y,u=find(x),v=find(y);
        double d=e[i].dist;
        if(u!=v){
            pa[u]=v;
            G[x].push_back(y);
            C[x].push_back(d);
            G[y].push_back(x);
            C[y].push_back(d);
            ret+=d;
            if(++cnt==N-1) break;
        }
    }
    return ret;
}
void DFS(int u,int fa,double facost){
    int L=node.size();
    for(int i=0;i<L;i++){
        int x=node[i];
        maxcost[x][u]=maxcost[u][x]=max(maxcost[x][fa],facost);
    }
    node.push_back(u);
    L=G[u].size();
    for(int i=0;i<L;i++){
        int v=G[u][i];
        if(v!=fa) DFS(v,u,C[u][i]);
    }
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i=0;i<N;i++) scanf("%d%d%d",&x[i],&y[i],&p[i]);
        double tot=MST();
        node.clear();
        memset(maxcost,0,sizeof(maxcost));
        DFS(0,-1,0);
        double ans=-1;
        for(int i=0;i<N;i++)
            for(int j=i+1;j<N;j++) ans=max(ans,(p[i]+p[j])/(tot-maxcost[i][j]));
        printf("%.2lf\n",ans);
    }
    return 0;
}


你可能感兴趣的:(uva1494 - Qin Shi Huang's National Road System 次小生成树)