Qin Shi Huang's National Road System( 次小生成树 )

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

 

题意:秦始皇要在n个城市之间修路,他想使得n个城市构成一个树形结构,并使得连接的路的长度和最小。这时,徐福说他有一种办法使得一条路不需要任何花销就能直接修成。秦始皇想让他修这些必要的路中最长的一条,但是徐福想修造福人口数最多的一条(即该条路连接的两个城市的人口和)。于是秦始皇为了均衡矛盾,给出了一种计算方法,将一条路两端的人口数A除以其他(n-2)条需要建造的道路的总长B,即计算A/B的比值,选取比值最大的一条修。输出该条路的比值。简而言之,该题就是要求除了徐福变的那条边既可以在秦始皇原定道路上,也可以不在,其他边都要在秦始皇原定道路上,然后要使徐福变的那条路A/B的值最大。

思路:先构建最小生成树,顺便存一下maxd方便之后使用,枚举所有边让他成为魔法变算一下此时的ans值,最后取一个最大值就是答案。

代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <math.h>

using namespace std;

const int maxn = 1005;
struct node {
    int from,to;
    double date;
    int via;
} a[2000005];
int n,m;
int pre[maxn];
int P[maxn];
int X[maxn];
int Y[maxn];
double maxd[maxn][maxn];
vector <int> G[maxn];

bool rule( node a, node b )
{
    return a.date<b.date;
}

int root( int x )
{
    if ( x!=pre[x] ) {
        x = root(pre[x]);
    }
    return pre[x];
}

double dis( int x1, int y1, int x2, int y2 )
{
    double ans = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
    return ans;
}

void kruskal()
{
    int i,j,k;
    for ( i=0; i<=n; i++ ) {
        pre[i] = i;
        G[i].clear();
        G[i].push_back(i);
    }
    sort(a,a+m,rule);
    double sum = 0;
    int tot = 0;
    for ( int i=0; i<m; i++ ) {
        if ( tot==n-1 ) {
            break ;
        }
        int x = root(a[i].from);
        int y = root(a[i].to);
        if ( x!=y ) {
            tot ++;
            sum += a[i].date;
            a[i].via = 1;
            int lenx = G[x].size();
            int leny = G[y].size();
            for ( j=0; j<lenx; j++ ) {
                for ( k=0; k<leny; k++ ) {
                    maxd[G[x][j]][G[y][k]] = maxd[ G[y][k] ][ G[x][j] ] = a[i].date;
                }
            }
            pre[x] = y;
            for ( j=0; j<lenx; j++ ) {
                G[y].push_back( G[x][j] );
            }
        }
    }

    double ans = 0;
    for ( i=1; i<=n; i++ ) {  // 枚举所有的魔法边
        for ( j=1; j<i; j++ ) {
            ans = max(ans, 1.0*(P[i]+P[j])/(sum-maxd[i][j]) );
        }  // maxd存的是两点间最长的一条边
    }
    printf("%.2f\n",ans);
}

int main()
{
    int listt,i,j;
    cin >> listt;
    while ( listt-- ) {
        cin >> n ;
        for ( i=1; i<=n; i++ ) {
            cin >> X[i] >> Y[i] >> P[i];
        }
        m = 0;
        for ( i=1; i<=n; i++ ) {
            for ( j=1; j<i; j++ ) {
                a[m].from = i;
                a[m].to = j;
                a[m].date = dis(X[i],Y[i],X[j],Y[j]);
                m ++;
            }
        }
        kruskal();
    }

    return 0;
}

 

题意转自:https://www.cnblogs.com/zk1431043937/p/7751599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值