1. 程式人生 > >HDU 4081秦皇修路(次小生成樹)

HDU 4081秦皇修路(次小生成樹)

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.

Ouput

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

關於題解推薦一篇部落格部落格連結,這篇部落格已經講得很透徹了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<memory.h>
#include<cmath>
#define M 1010
#define inf 99999999999.0
using namespace std;
double g[M][M],path[M][M];
//注意存在小數所以改成double
double dist[M],cost[M];
//cost為每個城市的人口
int pre[M],vis[M],r[M][2];
//用陣列r來儲存每個城市的座標,然後通過距離公式求出距離存在圖g中
bool used[M][M];//最小樹的標記陣列
int n,m;
double mst;
void init(){//初始化
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            g[i][j]=g[j][i]=inf;
        }
    }
    return;
}
void Found(){//距離公式計算出每條邊的邊長
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            g[i][j]=g[j][i]=sqrt((r[i][0]-r[j][0])*(r[i][0]-r[j][0])+(r[i][1]-r[j][1])*(r[i][1]-r[j][1]));
        }
    }
}
double prime(){
    double mst=0.0;//最小生成樹的值
    memset(path,0,sizeof(path));
    memset(vis,0,sizeof(vis));
    memset(used,0,sizeof(used));
    vis[1]=1;
    for(int i=1;i<=n;i++){
        dist[i]=g[1][i];
        pre[i]=1;//記錄字首
    }
    for(int i=1;i<n;i++){
        int u=-1;
        for(int j=1;j<=n;j++){
            if(!vis[j]){
                if(u==-1||dist[j]<dist[u]){
                    u=j;
                }
            }
        }
        used[u][pre[u]]=used[pre[u]][u]=true;//確定此邊已經使用過了
        mst+=g[pre[u]][u];
        vis[u]=1;
        for(int j=1;j<=n;j++){
            if(vis[j]&&j!=u){//path記錄了已經加入最小生成樹的點到u的最長路
                path[j][u]=path[u][j]=max(path[j][pre[u]],dist[u]);
            }
            if(!vis[j]){
                if(dist[j]>g[u][j]){//鬆弛
                    dist[j]=g[u][j];
                    pre[j]=u;
                }
            }
        }
    }
    return mst;
}
double second_tree(){
    double res=inf;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i!=j&&!used[i][j]){//次小生成樹
                res=min(res,mst-path[i][j]+g[i][j]);
            }
        }
    }
    return res;
}
int main(){
    int t;
    int x,y,z;
    cin>>t;
    while(t--){
        cin>>n;
        m=((n-1)*n)/2;
        init();
        memset(r,0,sizeof(r));
        memset(cost,0,sizeof(cost));
        for(int i=1;i<=n;i++){
            scanf("%d%d%lf",&r[i][0],&r[i][1],&cost[i]);
        }
        Found();
        mst=prime();
        double sec_mst=second_tree();
        double ratios= -1.0;
        for(int i=1; i<=n; ++i){
            for(int j=1; j<=n; ++j)if(i!=j){
                if(used[i][j]){
                    ratios=max(ratios, (cost[i]+cost[j])/(mst-g[i][j]));
                }
                else{
                    ratios=max(ratios, (cost[i]+cost[j])/(mst-path[i][j]));
                }
            }
        }
        printf("%.2f\n", ratios);
    }
    return 0;
}