题目链接:https://vjudge.net/problem/HDU-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): 8970    Accepted Submission(s): 3175

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
 
Recommend
lcy

题解:

问题:给出n个点及其坐标,求一棵生成树,使得A/B最大,其中A为生成树中某一条边(我们称之为魔法边)两端点的value和,B为生成树中除了上述边之外的其他边的权值之和。

看到了比率,还有生成树,第一反应是最优比率生成树。但再想想,最优比率生成树每次找答案都需要重置边权,而且好像也不能选定一条边之类的功能。总之,行不通。后来换了个角度思考,就柳暗花明了:

1.假定魔法边就是 u--v ,由于val[u]和val[v]是确定的,所以A/B中的A就已经确定了,为了使得A/B最大,我们要做的就是使得B最小,即使得除了魔法边之外的其他生成树边的权值和最小。所以我们就需要求出最小生成树。然后我们把u到v路径上边权最大的边删掉,然后用魔法边把u和v直接相连(两个操作之后仍然构成生成树)。这样,我们就求出了除了魔法边之外的其他生成树边的最小权值和。

2.有了上述结论,我们就可以用类似求次小生成树的方法,求出魔法边和A/B的最大值了。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; double cost[MAXN][MAXN], lowc[MAXN], Max[MAXN][MAXN];
bool vis[MAXN], used[MAXN][MAXN];
int x[MAXN], y[MAXN], val[MAXN], pre[MAXN]; double Prim(int st, int n)
{
double ret = ;
memset(vis, false, sizeof(vis));
memset(used, false, sizeof(used));
memset(Max, , sizeof(Max)); for(int i = ; i<=n; i++)
lowc[i] = (i==st)?:INF;
pre[st] = st; for(int i = ; i<=n; i++)
{
int k;
double minn = INF;
for(int j = ; j<=n; j++)
if(!vis[j] && minn>lowc[j])
minn = lowc[k=j]; ret += lowc[k];
vis[k] = true;
used[pre[k]][k] = used[k][pre[k]] = true;
for(int j = ; j<=n; j++)
{
if(vis[j] && j!=k)
Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]);
if(!vis[j] && lowc[j]>cost[k][j])
{
lowc[j] = cost[k][j];
pre[j] = k;
}
}
}
return ret;
} double SMST(double sum, int n)
{
double ret = ;
for(int i = ; i<=n; i++)
for(int j = i+; j<=n; j++)
ret = max(ret, (val[i]+val[j])/(sum-Max[i][j]));
return ret;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d",&n);
for(int i = ; i<=n; i++)
scanf("%d%d%d", &x[i], &y[i], &val[i]); for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
cost[i][j] = sqrt( 1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]) ); double sum = Prim(, n);
double ans = SMST(sum, n);
printf("%.2f\n", ans);
}
}

HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形的更多相关文章

  1. hdu4081 Qin Shi Huang's National Road System 次小生成树

    先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...

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

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

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

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

  5. HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏

    Qin Shi Huang's National Road System                                                                 ...

  6. HDU4081: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 ...

  7. hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)

    题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: ...

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

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

  9. HDU4081 Qin Shi Huang's National Road System

    先求最小生成树 再遍历每一对顶点,如果该顶点之间的边属于最小生成树,则剪掉这对顶点在最小生成树里的最长路径 否则直接剪掉连接这对顶点的边~ 用prim算法求最小生成树最长路径的模板~ #include ...

随机推荐

  1. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

  2. zoj 2886 Look and Say

    Look and Say Time Limit: 2 Seconds      Memory Limit: 65536 KB The look and say sequence is defined ...

  3. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  4. 【搜索】codeforces C. The Tag Game

    http://codeforces.com/contest/813/problem/C [题意] 给定一棵有n个结点的树,初始时Alice在根结点1,Bob在非根结点x; Alice和Bob轮流走,每 ...

  5. github新建本地仓库,再同步远程仓库基本用法

    github新建本地仓库,再同步远程仓库基本用法 1 mkdir gitRepo 2 cd gitRepo 3 git init  #初始化本地仓库 4 git add xxx  #添加要push到远 ...

  6. Codevs 2756 树上的路径

    2756 树上的路径  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 给出一棵树,求出最小的k,使得,且在树中存在 ...

  7. P1546||2627 最短网络 Agri-Net 洛谷||codevs

    https://www.luogu.org/problem/show?pid=1546 同http://codevs.cn/problem/2627/ 不同算法 题目背景 农民约翰被选为他们镇的镇长! ...

  8. canvas仿芝麻信用分仪表盘

    这是一个仿支付宝芝麻信用分的一个canvas,其实就是一个动画仪表盘. 首先, 上原图: 这个是在下支付宝上的截图,分低各位见笑了.然后看下我用canvas实现的效果图: <canvas id= ...

  9. django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务

    上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑.今天我们利用markdown添加富文本支持. markdown语法说明: h ...

  10. 【APUE】用户态与内核态的区别

    当一个任务(进程)执行系统调用而陷入内核代码中执行时,我们就称进程处于内核运行态(或简称为内核态).此时处理器处于特权级最高的(0级)内核代码中 执行.当进程处于内核态时,执行的内核代码会使用当前进程 ...