HDU4081-次小生成树

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10520    Accepted Submission(s): 3708


 

Problem 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.

扫描二维码关注公众号,回复: 9513841 查看本文章

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最大,就是A 大  B 小。

思路:我们可以先用最小生成树算出最小的B的值,然后找出免费修的那条路。免费修的路有两种情况,一种是在最小生成树里的一条边,还一种是不在树里面的边,因为要值的和最大,所以我们要区别一下。所以这里我们可以引用次小生成树来解决这个问题。

坑点:提交的时候是这样的,1 2 3发是t了,之后我把map初始化删了,RE了。。。数组开小了,我开大一点之后A了,然后我把初始化加进去也A 了。。。真实的测评姬。然后注意一下变量的类型

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=1100;
const int INF=999999999;
double map[maxn][maxn],Max[maxn][maxn],dis[maxn];
int pre[maxn];
bool used[maxn][maxn];
int n;
struct Node
{
    int x,y;
    int sum;
}num[maxn];
void Init()
{
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i==j) map[i][j]=0;
            else map[i][j]=INF*1.0;
    for (int i=1;i<=n;i++)
        pre[i]=1;
    pre[1]=0;
    memset(used,false,sizeof(used));
    return ;
}
double cal(Node a,Node b)
{
    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
}
double prim()
{
    int i,j;
    double sum=0;
    bool vis[maxn];
    memset(Max,0,sizeof(Max));
    memset(vis,false,sizeof(vis));
    for (i=1;i<=n;i++)
        dis[i]=map[1][i];
    vis[1]=true;dis[1]=0;
    for (i=2;i<=n;i++)
        {
            double Min=INF*1.0;
            int flag;
            for (j=1;j<=n;j++)
                if (vis[j]==true)
                    continue;
                else if (Min>dis[j])
                        {
                            Min=dis[j];
                            flag=j;
                        }
        //    cout<<Min<<" - "<<flag<<endl;
    //        for (j=1;j<=n;j++)
    //            cout<<dis[j]<<" ";
    //        cout<<endl;
            if (Min==INF)
                return -1;
            sum=sum+Min;
            vis[flag]=true;
            used[flag][pre[flag]]=used[pre[flag]][flag]=true;
            for (j=1;j<=n;j++)
                {
                    if (vis[j]==true&&j!=flag)  //这里要一个j!=flag是因为max[flag][flag]不要赋值,不然对后面的有影响
                        Max[j][flag]=Max[flag][j]=max(Max[j][pre[flag]],dis[flag]);
        //            cout<<dis[j]<<" "<<map[flag][j]<<endl;
        //            cout<<vis[j]<<"-"<<j<<endl;
                    if (vis[j]==false&&dis[j]>map[flag][j])
                        {
                            dis[j]=map[flag][j];
                            pre[j]=flag;
                        }
                }
        }
    return sum;
}
int main()
{
    int t,i,j;
    cin>>t;
    while (t--)
        {
            cin>>n;
    //        cout<<"n=="<<n<<endl;
            Init();
            for (i=1;i<=n;i++)
                scanf("%d%d%d",&num[i].x,&num[i].y,&num[i].sum);
                //    cout<<i<<endl;
                
        //    cout<<"input"<<i<<endl;
            for (i=1;i<=n;i++)
                for (j=i+1;j<=n;j++)
                    map[i][j]=map[j][i]=cal(num[i],num[j]);
    /*        for (i=1;i<=n;i++)
                for (j=1;j<=n;j++)
                    if (j==n) cout<<map[i][j]<<endl;
                    else cout<<map[i][j]<<" ";*/
            double B=prim();
            double A=-1;
        //    cout<<B<<endl;
            for (i=1;i<=n;i++)
                for (j=i+1;j<=n;j++)       //这里就是判断两点之间的边有没有走过,然后找出最大值
                    if (used[i][j]==true) A=max(A,1.0*(num[i].sum+num[j].sum)/((B-map[i][j])*1.0));
                    else A=max(A,1.0*(num[i].sum+num[j].sum)*1.0/((B-Max[i][j])*1.0));
            printf("%.2lf\n",A);
        }
    return 0;
}
发布了46 篇原创文章 · 获赞 2 · 访问量 3214

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/86596484