hdu 4081 Qin Shi Huang's National Road System

本文探讨了一个基于秦始皇历史背景的算法问题,通过构建最小生成树来优化路网结构,确保整体路径长度最短的同时,使得特定路径的人口效益最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-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<<"---------------"<<endl
const 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;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值