HDU 4081(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 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

题目描述:在图中求出一个联通图,然后在联通图中选出一条边,设这条边连接的城市中的人口数量之和为A,除了这条边之外的所有边的和(该联通图中,即能够使得所有点相连的边)为B,使得A/B的值最大。
解题思路:
对于每条边连接的城市的人口数量总和我们是可以通过枚举得到的,并且当确定了边之后我们是可以固定这个人口数量的,那么我们要使得A/B的值最大,那么我们要保证B的值,也就是在该连通图中这条边之外的所有边的权值和最小,然后我们就可以运用最小生成树算法来求的边权和最小的连通图啦,然后我们枚举这条特殊的边,这条边连接的城市的人口总数是可以确定的,然后我们就相当于加入了这条边,在最小生成树中,如果这条边没有在最小生成树中就会形成环,这样会使边权和变大,所以我们减去这个环中的边权最大的边,这样剩下的边权和就是最小的啦。
代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
const int maxn=1e6+7;
const int inf=0x3f3f3f3f;
int read(int &x)
{
    x=0;
    int f=1;
    char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c<='9' && c>='0'){
        x=x*10+c-'0';
        c=getchar();
    }
    x*=f;
}
struct node
{
    int x,y,val;
}e[maxn];
int pre[1100],vis[1100],use[1100][1100],n;
double maxx[1100][1100],maze[1100][1100],dis[1100];
double prim()
{
    for(int i=1;i<=n;i++){
        dis[i]=maze[1][i];
        vis[i]=0;
        pre[i]=1;
    }
    double ans=0;
    vis[1]=1;
    memset(maxx,0,sizeof maxx);
    memset(use,0,sizeof use);
    for(int i=1;i<n;i++){
        int u=-1;
        double minn=1e9;
        for(int j=1;j<=n;j++){
            if(!vis[j] && dis[j]<minn){
                u=j;minn=dis[j];
            }
        }
        if(u==-1)return -1;
        vis[u]=1;
        ans+=minn;
        use[pre[u]][u]=use[u][pre[u]]=1;
        for(int j=1;j<=n;j++){
            if(j==u)continue;
            if(vis[j])maxx[u][j]=maxx[j][u]=max(maxx[pre[u]][j],dis[u]);
            else{
                if(dis[j]>maze[u][j]){
                    dis[j]=maze[u][j];
                    pre[j]=u;
                }
            }
        }
    }
    return ans;
}
double comb(int i,int j)
{
    double ans=((e[i].x-e[j].x)*(e[i].x-e[j].x)+(e[i].y-e[j].y)*(e[i].y-e[j].y));
    return ans;
}
double solve(double sum)
{
    double res=0;
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            int x=e[i].val+e[j].val;
            if(use[i][j]) res=max(res,x*1.0/(sum-maze[i][j]));
            else res=max(res,x*1.0/(sum-maxx[i][j]));
        }
    }
    return res;
}
int main()
{
    int t;
    read(t);
    while(t--){
        read(n);
        for(int i=1;i<=n;i++){
            int x,y,v;
            read(x),read(y),read(v);
            e[i]=node{x,y,v};
        }
        for(int i=1;i<=n;i++){
            maze[i][i]=0;
            for(int j=i+1;j<=n;j++){
                maze[i][j]=maze[j][i]=sqrt(comb(i,j));
            }
        }
        double ans=prim();
        printf("%.2lf\n",solve(ans));
    }
    return 0;
}
发布了34 篇原创文章 · 获赞 3 · 访问量 247

猜你喜欢

转载自blog.csdn.net/qq_44641782/article/details/103230853
今日推荐