【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路

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.

Solution

枚举加特效的一条边(u,v),然后通过预处理实现O(1)得到(u,v)上最大边。

白书例题,类似次小生成树的运用。

Code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn=1005;
 7
 8 int f[maxn][maxn],p[maxn];
 9 int x[maxn],y[maxn],c[maxn];
10 int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
11 struct edge{
12     int u,v,w;
13     bool operator<(const edge&a)
14         const {return w<a.w;}
15 }g[maxn*maxn];
16 int head[maxn],e[maxn*2],w[maxn*2],nxt[maxn*2],k;
17 void adde(int u,int v,int g){
18     e[++k]=v;w[k]=g;nxt[k]=head[u];head[u]=k;
19     e[++k]=u;w[k]=g;nxt[k]=head[v];head[v]=k;
20 }
21 int dist(int a,int b){
22     return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
23 }
24 int n,m;
25
26 int q[maxn],clock;
27 void dfs(int p,int u){
28     q[++clock]=u;
29     for(int i=head[u];i;i=nxt[i]){
30         int v=e[i];
31         if(v==p) continue;
32         for(int j=1;j<=clock;j++)
33             f[v][q[j]]=f[q[j]][v]=max(f[u][q[j]],w[i]);
34         dfs(u,v);
35     }
36 }
37
38 void clear(){
39     m=k=clock=0;
40     memset(head,0,sizeof(head));
41     memset(e,0,sizeof(e));
42     memset(w,0,sizeof(w));
43     memset(nxt,0,sizeof(nxt));
44     memset(f,0,sizeof(f));
45 }
46
47 int main(){
48     int T;
49     scanf("%d",&T);
50     while(T--){
51     clear();
52     scanf("%d",&n);
53     for(int i=1;i<=n;i++)
54         scanf("%d%d%d",&x[i],&y[i],&c[i]),p[i]=i;
55
56     for(int i=1;i<=n;i++)
57         for(int j=i+1;j<=n;j++){
58             m++;
59             g[m].u=i,g[m].v=j;
60             g[m].w=dist(i,j);
61         }
62     sort(g+1,g+m+1);
63
64     double sum=0;
65     for(int i=1;i<=m;i++){
66         int x=find(g[i].u),y=find(g[i].v);
67         if(x!=y){
68             adde(g[i].u,g[i].v,g[i].w);
69             sum+=sqrt(g[i].w);
70             p[x]=y;
71         }
72         if(k==2*(n-1)) break;
73     }
74
75     dfs(0,1);
76
77     double ans=0;
78     for(int u=1;u<=n;u++)
79         for(int v=u+1;v<=n;v++){
80             double ansx=sum;
81             ansx-=sqrt(f[u][v]);
82             ansx=(c[u]+c[v])*1.0/ansx;
83             ans=max(ans,ansx);
84         }
85     printf("%.2lf\n",ans);
86     }
87     return 0;
88 }

【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路

时间: 2024-07-30 00:23:38

【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路的相关文章

UVALive 5713 Qin Shi Huang&#39;s National Road System秦始皇修路(MST,最小瓶颈路)

题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最大.问A/B最大是多少?(1000个城市) 思路: 老徐可免费修得1条路,那么剩下最多也只需要修n-2条路了,这n-2条路要尽量挑短的,而老徐的那条无所谓长短,只要两城人口尽量多即可.这是没有什么贪心策略的,因为老徐所修之路会影响MST的权值之和的大小.穷举所有城市对要O(n*n),再求次MST需要

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

分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成树然后查询就好了 然后预处理那个数组是O(n^2)的,这样总时间复杂度是O(n^2+m) 这是因为这个题n比较小,如果n大的时候,就需要路径查询了,比如LCA 或者树链剖分达到O(mlogn) #include <iostream> #include <algorithm> #incl

hdu 4081 Qin Shi Huang&#39;s National Road System(最小生成树+dp)

同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游方道士突然出现,他说他可以不消耗任何人力财力,使用法术凭空造一条路,路的长度无所谓,但是只能造一条.那么问题来了,徐福希望将两座人口数最多的城市连接起来,而秦始皇希望将最长的路修好.最后折中了一下, 将A/B最大的一条路用法术修出来.其中A是两座城市的人口和,B是除了用法术修的路以外,其它需要修建的

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中的边一定

HDU4081Qin Shi Huang&#39;s National Road System(最小生成树+DFS)

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

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树+暴力枚举边)

题目大意:给你1000个点,每个点上有一个数目代表这个城市有多少人,让你把这N个点构成一颗生成树,你可以删除其中的任意一条边.让你求出一个比例A/B是的这个比例最大,A表示你删除那条边上两个城市的人口数之和,B表示的是去掉这条变这可生成树上其他的边的总长度. 解体思路:先求出来最小生成树,然后暴力枚举生成树的边,B=总数-这条边的长度.A = 将这条连断开之后左右集合中权值最大的两个数的和. 这样保证了B最小的情况下,去找最大的A,所以是可行的解.生成树的同时建边,然后dfs找最大值. PS:这

HDU4081 Qin Shi Huang&#39;s National Road System【prim最小生成树+枚举】

先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.由于结果是A/B,A的变化会引起B的变化,两个制约,无法直接贪心出最大的A/B,故要通过枚举 2.不管magic road要加在哪里,加的边是否是最小生成树上的边,都会产生环,我们都要选择一条边删掉 注意删掉的边必须是树的环上的边,为了使结果最大,即找出最大的边 3.可以枚举两点,找出边,也可以枚举边,找出点,我是用后者,感觉比较容易写也好理解 #include <cstdio&g

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

今天比赛AC的一道最小生成树的题目 , 学到了不少东西 . 最小生成树的模板很简单,最简洁好写的还是lrj紫书上的代码 .利用并查集加速算法 . 该题的不同之处在于它选择任意一条路修成"魔法"道路 , 然后其他路的权值之和还要是最小的一棵次小生成树,并且求魔法道路两端点值之和除以其他路径长之和的最大值 . 显然该题的难点在于枚举两个端点之后怎么快速的求出次小生成树权值之和 .  枚举两个端点之后的复杂度已经是O(n^2),所以要想出一个快速的方法 . 受紫书上例题uva 1151 (传

HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树+倍增求LCA

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5428    Accepted Submission(s): 1902 Problem Description