[hdu P4081] Qin Shi Huang’s National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

Source
2011 Asia Beijing Regional Contest

一道很不错的题目。

题目大意:

(不想写了,应用一下zk大佬的吧)秦始皇要在n个城市之间修路,他想使得n个城市构成一个树形结构,并使得连接的路的长度和最小。这时,徐福说他有一种办法使得一条路不需要任何花销

就能直接修成。秦始皇想让他修这些必要的路中最长的一条,但是徐福想修造福人口数最多的一条(即该条路连接的两个城市的人口和)。于是秦始皇为了均衡矛盾,给出了一种计算方法,将一条路两端

的人口数A除以其他(n-2)条需要建造的道路的总长B,即计算A/B的比值,选取比值最大的一条修。输出该条路的比值。简而言之,该题就是要求除了徐福变的那条边既可以在秦始皇原定道路上,也可以不

在,其他边都要在秦始皇原定道路上,然后要使徐福变的那条路A/B的值最大。

我们仔细思考,对于每一条路,其A值是固定的,我们要让其B值最小化。

那我们会想到与MST有点关联。那我们先构造出MST,设权值和为sum。

构造出MST后,对于每一条边:

如果在原MST上,则:ans=max(ans,(p[x]+p[y])/(sum-dis(x,y));

如果不在原MST上,怎么办呢?

由于我们要强制将这条边加入生成树,而我们又要维持生成树只有n-1条边的性质,所以我们就要删掉一条边,且这条边满足删去以后保持生成树且边权值最大。

那这就是一个次小生成树的模型了。

关于次小生成树:

次小生成树

code:

 #include<bits/stdc++.h>
 #define sqr(x) ((x)*(x))
 #define ms(a,x) memset(a,x,sizeof a)
 ;
 using namespace std;
 int n,x[N],y[N],p[N],pre[N];
 double f[N],d[N][N],g[N][N],sum,ans;
 bool vis[N],used[N][N];
 inline int read() {
     ; char ch=getchar();
     ') ch=getchar();
     +ch-',ch=getchar();
     return x;
 }
 double dis(int a,int b) {
     return sqrt(1.0*sqr(x[a]-x[b])+1.0*sqr(y[a]-y[b]));
 }
 int main() {
     for (int T=read(); T; T--) {
         n=read(),sum=;
         ; i<=n; i++)
             x[i]=read(),y[i]=read(),p[i]=read();
         ; i<=n; i++)
             ; j<=n; j++) d[i][j]=dis(i,j);
         ms(g,),ms(pre,),ms(used,),ms(vis,),vis[]=;
         ; i<=n; i++)
             f[i]=g[][i]=d[][i],pre[i]=;
         for ( ; ; ) {
             ; double minor=1e18;
             ; i<=n; i++)
                 if (!vis[i]&&minor>f[i]) k=i,minor=f[i];
             ) break;
             vis[k]=,sum+=minor;
             used[k][pre[k]]=used[pre[k]][k]=;
             ; i<=n; i++) {
                 if (vis[i]&&i!=k) g[k][i]=g[i][k]=max(f[k],g[i][pre[k]]);
                 if (!vis[i]&&f[i]>d[k][i]) f[i]=d[k][i],pre[i]=k;
             }
         }
         ans=;
         ; i<n; i++)
             ; j<=n; j++)
             if (used[i][j]) ans=max(ans,1.0*(p[i]+p[j])/(sum-d[i][j]));
             else ans=max(ans,1.0*(p[i]+p[j])/(sum-g[i][j]));
         printf("%.2lf\n",ans);
     }
     ;
 }

[hdu P4081] Qin Shi Huang’s National Road System的更多相关文章

  1. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  2. hdu 4081 Qin Shi Huang's National Road System (次小生成树)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  3. hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)

    题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...

  4. HDU 4081 Qin Shi Huang's National Road System 次小生成树变种

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  7. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

  8. hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

  9. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

随机推荐

  1. SharePoint开启错误提示

    1,打开80下面的Web.config文件2,CallStack="true" 和 <customErrors mode="Off" /> < ...

  2. 苹果cms安装及配置详细教程

    听说这个好!php+mysql的 下载 http://www.maccms.com/down.html 下载之后解压到你的网站跟目录中,就像这个样子的   后台目录 然后重要的一步来了,在ftp工具上 ...

  3. 小程序map组件默认层级最高,并且不能设置的解决方案

    map组件默认在最上面,若要设置像ofo那样的按钮有两个方法,一是用控件设置,控件就是controls属性,控件只能显示图片,不能显示文字之类的.二是用cover-view组件,这个组件就是悬浮在一些 ...

  4. lua常用方法收集

    1. xlua之将c#集合转换成table -- 将c#的list转换成table local function ConvertCSListToTable(list) local t = {}; , ...

  5. protocol buffer 编码

    protocol buffer能够跨平台提供轻量的序列化和反序列化,得益于其平台无关的编码格式,本文就介绍下其中的编码格式. Varints 在protocol buffer中大量使用到了Varint ...

  6. Mysql模糊查询like效率,以及更高效的写法

    在使用msyql进行模糊查询的时候,很自然的会用到like语句,通常情况下,在数据量小的时候,不容易看出查询的效率,但在数据量达到百万级,千万级的时候,查询的效率就很容易显现出来.这个时候查询的效率就 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. Python自然语言处理笔记【二】文本分类之监督式分类的细节问题

    一.选择正确的特征 1.建立分类器的工作中如何选择相关特征,并且为其编码来表示这些特征是首要问题. 2.特征提取,要避免过拟合或者欠拟合 过拟合,是提供的特征太多,使得算法高度依赖训练数据的特性,而对 ...

  9. linux 安装nginx+php+mysql

    http://www.cnblogs.com/kyuang/p/6801942.htmlnginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库 ...

  10. 剑指offer(14)链表中倒数第K个节点

    题目描述 输入一个链表,输出该链表中倒数第k个节点. 题目分析 用两个指针来跑,两个指针中间相距k-1个节点,第一个指针先跑,跑到了第k个节点时,第二个指针则是第一个节点. 这时候两个一起跑.当第一个 ...