最小生成树 次小生成树 hdu4081

A - Qin Shi Huang‘s National Road System

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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.

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

应用时:2h,如果第二次看到这个题就应当用45min-1h30min

实际用时:1days19h

经验教训:不要被表面吓住了..其实就是那么简单的东西

思路:

我们看到这个就想到最小生成树,那么首先假设T是最小生成树,权值和为allt,这里就有一非常需要熟用的最小生成树性质,即:

对于任意d[i][j]为题目所给的ij距离,有两种情况

1 d[i][j]在最小生成树内

2 i,j由最小生成树上的边 (i,x1),(x1,x2)...(xk,j)组成,那么每个中间路径上的边都有d[xk][xk+1]<d[i][j]要不然d[i][j]就在里面了

那么现在我们假设取的就是i,j边,那么就相当于是添加了一条边,d[i][j]=0,

1 当d[i][j]在最小生成树内的时候,相当于直接更新了最小生成树,allt-d[i][j]即可,所有中间路径为d[i][j]的仍然是最小生成树

2 当d[i][j]不在最小生成树内的时候,这时添加d[i][j]=0形成了环,(其实第一种情况也是环,)这时多连了一条边,于是我们想到要把最大的那条边取出来,取出来肯定是树,不在以i为根的子树上的点不需要变化,对于以i为根的子树,之前不在里面的边现在还是不在里面,不然的话子树的权值反而在增大,那么对于这个环肯定是删掉最大边形成最小子树

那么如何求出最小生成树上从i到j的路径所必须经过的最大边呢?用个dp就行了

dp[i][j]=max(dp[i][i的前驱节点],dp[i][j]);//假设i先加入最小生成树

这道题还有各种姿势:这个像次小生成树,还有枚举各边,删除这条边的话在两边的树上取人口最大的两个城市,或者kruscal()直接求最大边,不过不想写了

#include <cstdio>
#include <cstring>
#include  <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1105;
int n;
struct P{
    int x,y,p;
}v[maxn];//记录城市
double d[maxn][maxn];//城市之间的给定距离
bool vis[maxn];//记录prim中间过程
double maxd[maxn][maxn];//用于记录在树上从i到j需要经过的最大边
typedef pair<int ,int > point;//记录从second到first的一条边
typedef pair<double,point> pr;//first 距离 second 起终点

priority_queue<pr,vector<pr>,greater<pr> >que;//用来prim取最小权值边
double prim(){
    memset(vis,0,sizeof(vis));
    vis[0]=true;//起点定为0已经在树上了
    int num=1;//当前最小生成树中的节点数
    while(!que.empty())que.pop();//多组case
    double ans=0;//记录总边权
    for(int i=1;i<n;i++){
        que.push(pr(d[0][i],point(i,0)));//把与0相连的加入prim的优先队列
    }
    while(num<n){
        double td=que.top().first;//取信息
        int  t=que.top().second.first;
        int f=que.top().second.second;
        que.pop();
      if(vis[t])continue;//如果已经加入了那么就不用管了

        vis[t]=true;num++;ans+=td;//加点加边

        maxd[t][f]=maxd[f][t]=td;//dp求最大边的起点,这两点直接相连

        for(int i=0;i<n;i++){
            if(!vis[i]){
                que.push(pr(d[t][i],point(i,t)));//不在里面的继续更新
            }
            else {
                if(i!=t){
                    maxd[t][i]=maxd[i][t]=max(maxd[f][i],td);//在里面的dp求值
                }
            }
        }
    }

    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].p);
        }
        for(int i=0;i<n;i++){//建图
            for(int j=0;j<=i;j++){
                d[i][j]=d[j][i]=sqrt((v[i].x-v[j].x)*(v[i].x-v[j].x)+(v[i].y-v[j].y)*(v[i].y-v[j].y));
            }
        }
        double allt=prim();
        double maxrate=-1;
        for(int i=0;i<n;i++){//取所有可能的城市对
            for(int j=0;j<i;j++){
                double rate=(v[i].p+v[j].p)/(allt-maxd[i][j]);
                maxrate=max(maxrate,rate);
            }
        }
        printf("%.2f\n",maxrate);
    }
    return 0;
}

  

最小生成树 次小生成树 hdu4081,布布扣,bubuko.com

时间: 2024-10-13 01:46:24

最小生成树 次小生成树 hdu4081的相关文章

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.现在要在所有城市之间修路,保证每个城市都能相连,并且保证A/B 最大,所有路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i以外所有路的花费的和(路径i的花费为0). 分析: 先求一棵最小生成树,然后枚举每一条最小生成树上的边,删掉后变成两个生成树,然后找两个集合中点权最大的两 个连接起来.这两个点中必然有权值最大的那个点,所以直接从权值最大的点开始dfs. 为了使A/B的值最大,则A尽可能大,B尽可能小.所以B中的边一定

POJ 1679 The Unique MST 【最小生成树/次小生成树】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

HDU4081 Qin Shi Huang&#39;s National Road System【Kruska】【次小生成树】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3979    Accepted Submission(s): 1372 Problem Description During the Warring States Period of ancient China(4

HDU4081 Qin Shi Huang&#39;s National Road System(次小生成树)

枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出MST上任意两点路径上的最长边d[i][j]. 当(u,v)是magic road时, 如果它在原本的MST上,则B就等于s-原(u,v)的权,而原(u,v)的权其实就是d[u][v]: 如果它不在原本的MST上,则B就等于s-d[u][v]+0. 总之就是一个式子:B=s-d[u][v]. 于是,在

hdu4081 Qin Shi Huang&#39;s National Road System 次小生成树

先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后慢慢融会贯通.我要走的路还长着啊. 讲讲次小生成树的思想吧.首先,在prim算法中做一些改变,求出任意两点(u,v)路径之间的最大权值,并记录,记为maxe[u][v].运行遍prim算法.枚举每一条边,如果该边是属于最小生成树的边,则ratio=(value(u) +value(v) ) /( a

HDU4081 Qin Shi Huang&#39;s National Road System 【次小生成树】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3395    Accepted Submission(s): 1189 Problem Description During the Warring States Period of ancient China(4

hdu4081 次小生成树变形

http://acm.hdu.edu.cn/showproblem.php?pid=4081 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

HDU-4081.Qinshihuang&#39;sNationalRoadSystem(次小生成树变种)

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10874    Accepted Submission(s): 3846 Problem Description During the Warring States Period of ancient China(4

poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22346   Accepted: 7924 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G =