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.

题意:

这个题目和次小生成树比较类似,题目意思就是,在n个点的联通图中每条边有权值(路径长),每个点也有权值(人口数)。
这时可以选一条边使求生成树 权值B 时不算上该边,该边的两端点权值之和为A(人口数和),现在求一个生成树的方案,保证A/B最大。

解法:

大致解法就是枚举每条边为边权值不计入生成树的那条边Pi,然后判断如果该边在最小生成树上的话,最优解就是sum-Pi;
否则就是减去把这个边加入次小生成树之后,生成的环上面的除该边外的最大的那条边。
类似于次小生成树,先用prim算法求最小生成树,建树同时设dp[i][j]表示的是生成树中从i到j的最大的路径上的最长一条边。
dp[i][j]=max(dp[i][pre[Pu]],Pu),Pu表示每个点,pre[Pu]表示prim过程中连接Pu点的边的另外一个端点

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
#define N 1000
using namespace std;
struct node
{
    double x,y,people;
};
node maps[1000+20];
int n;
double grap[N+20][N+20], path[N+20][N+20];
bool tree[N+20][N+20];
double val[N+20];
int pre[N+20];
double sum;
bool vis[N+20];
void prim()
{
    memset(vis,0,sizeof(vis));
    memset(tree,0,sizeof(tree));
    memset(pre,0,sizeof(pre));
    memset(path,0,sizeof(path));
    memset(val,0,sizeof(val));
    for(int i=1;i<=n;i++)
    {
        val[i]=grap[1][i];
        pre[i]=1;
    }
    vis[1]=true;
    sum=0.0;
    int T=n-1;
    while(T--)
    {
        double minn= (double)2147483647 ;
        int index=0;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==false&&minn>val[j])
            {
                minn=val[j];
                index=j;
            }
        }
        tree[pre[index]][index]=tree[index][pre[index]]=true;
        sum+=minn;
        vis[index]=true;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==true&&index!=j)//wa点之一,index==j的时候会勿误取minn
            {
                path[index][j]=path[j][index]=max(path[j][pre[index]],minn);
            }

            if(vis[j]==false&&grap[index][j]<val[j])
            {
                val[j]=grap[index][j];
                pre[j]=index;
            }
        }
    }
    //cout<<sum<<endl;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(maps,0,sizeof(maps));
        memset(grap,0,sizeof(grap));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&maps[i].x,&maps[i].y,&maps[i].people);
        }

        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                grap[j][i]=grap[i][j]=sqrt((maps[i].x-maps[j].x)*(maps[i].x-maps[j].x)+(maps[i].y-maps[j].y)*(maps[i].y-maps[j].y));
        prim();

        double ans=0.0;
        double temp;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(tree[i][j]==true)
                    temp=(maps[i].people+maps[j].people)/(sum-grap[i][j]);
                else
                    temp=(maps[i].people+maps[j].people)/(sum-path[i][j]);
                ans=max(temp,ans);
            }
        }
        printf("%.2f\n",ans);
    }
    return 0;
}

P.S.发现设变量好别扭啊。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值