HDU 4081Qin Shi Huang's National Road System(次小生成树)
题目大意:
有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点。秦始皇希望这所有n-1条路长度之和最短。然后徐福突然有冒出来,说是他有魔法,可以不用人力、财力就变出其中任意一条路出来。
秦始皇希望徐福能把要修的n-1条路中最长的那条变出来,但是徐福希望能把要求的人力数量最多的那条变出来。对于每条路所需要的人力,是指这条路连接的两个城市的人数之和。
最终,秦始皇给出了一个公式,A/B,A是指要徐福用魔法变出的那条路所需人力, B是指除了徐福变出来的那条之外的所有n-2条路径长度之和,选使得A/B值最大的那条。
分析:
A/B 要最大 那么 B 就应该最小,为了使 B 最小,可以求最小生成树,去掉权值最大的边,对于找权值最大边,两重循环枚举即可,如果该边在最小生成树上,直接去掉,如果不在最小生成树上,必然会产生一个环,那么去掉这一个环上的最大边,这就是次小生成树的算法。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = + ;
int po[Max], X[Max], Y[Max];
int T, n;
double G[Max][Max], maxlen[Max][Max];
bool used[Max], eused[Max][Max];
double mincost[Max];
int mincostfrom[Max];
double prime()
{
memset(used, , sizeof(used));
memset(eused, , sizeof(eused));
memset(maxlen, , sizeof(maxlen));
double ans = ;
mincostfrom[] = ; // 一个边有两个顶点,这个mincost[i] = j,表示 j - i 这样一条边
for (int i = ; i < n; i++)
mincost[i] = INF;
mincost[] = ;
int added = ;
while (added < n)
{
double cost = (double) INF;
int from, to;
for (int t = ; t < n; t++)
{
if (used[t] == && cost > mincost[t])
{
from = mincostfrom[t];
to = t;
cost = mincost[t];
}
}
added++;
ans += cost;
eused[from][to] = eused[to][from] = ; // 在最小生成树上
used[to] = ;
for (int i = ; i < n; i++)
{
if (used[i] && to != i)
maxlen[i][to] = maxlen[to][i] = max(maxlen[i][from], max(maxlen[i][to], G[from][to])); // maxlen[i][j] 表示 i 和 j之间权值最大边,找到一个边就要更新
}
for (int i = ; i < n; i++)
{
if (!used[i] && G[to][i] < mincost[i])
{
mincost[i] = G[to][i];
mincostfrom[i] = to;
}
}
}
return ans;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d%d%d", &X[i], &Y[i], &po[i]);
}
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
double dis = sqrt((double) (X[j] - X[i]) * (X[j] - X[i]) + (double) (Y[j] - Y[i]) * (Y[j] - Y[i]));
G[i][j] = G[j][i] = dis;
}
}
double mst = prime();
//cout << mst << endl;
double ans = ;
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
double popu = po[i] + po[j];
double tollen;
if (eused[i][j])
{
tollen = mst - G[i][j];
}
else
{
tollen = mst - maxlen[i][j];
}
ans = max(ans, popu / tollen);
}
}
// cout << ans << endl;
printf("%.2f\n", ans);
}
return ;
}
HDU 4081Qin Shi Huang's National Road System(次小生成树)的更多相关文章
- 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 ...
- 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 ...
- HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形
题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...
- HDU 4081 Qin Shi Huang's National Road System [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
- hdu4081 Qin Shi Huang's National Road System 次小生成树
先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- [hdu P4081] Qin Shi Huang’s National Road System
[hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 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: ...
- 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 ...
随机推荐
- android Animation介绍
Animation介绍: 在Android SDK介绍了2种Animation模式: 1. Tween Animation:间动画,通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即 ...
- Android 多个listview的实现
正好,今天项目中需要,先写了个demo,给大家参考参考. 先上图,需要的自己,看看具体的代码实现步骤 大概说一下实现步骤: 1.布局中先用 scrollview 包裹 LinearLayout < ...
- 简单好记的Jdk 环境变量配置
- Lucene提供的条件判断查询
第一.按词条搜索 - TermQuery query = new TermQuery(new Term("name","word1"));hits = sear ...
- Curator框架的使用
Curator框架的目的是减少用户的复杂度,毕竟原生的Zookeeper难以使用. 这里举一个使用例子. 第一步:建立连接 // 以下代码与192.168.1.101:2181建立了连接Curator ...
- SE Springer小组之《Spring音乐播放器》可行性研究报告三、四
3 对现有系统的分析 由于本次可行性分析主要是建立在团队自行实现一个音乐软件的目标上,并不是在一个现有系统的基础上开发改进的新系统.因此这里将分析一款市面上已经存在的音乐软件(以下称为W音乐),并为之 ...
- Linux运维之基础拾遗
第一部分 Linux常用文件管理命令 1.1 cp 文件复制 常用选项 -i # 覆盖之前提醒用户确认 -f # 强制覆盖目标文件 -r # 递归复制目录 -d # 复制符号链接本身而非其指向的源文件 ...
- k近邻算法(knn)的c语言实现
最近在看knn算法,顺便敲敲代码. knn属于数据挖掘的分类算法.基本思想是在距离空间里,如果一个样本的最接近的k个邻居里,绝大多数属于某个类别,则该样本也属于这个类别.俗话叫,"随大流&q ...
- 当AngularJS POST方法碰上PHP
问题描述 怎么POST过去给PHP都收不到资料? $_POST方法取不到正确的传入值! 原理说明 AngularJS这套framework使用的AJAX方法中,资料传递的格式为JSON,送出去的hea ...
- Codeforces Round #382(div 2)
A.= = B. 题意:给出n个数和n1和n2,从n个数中分别选出n1,n2个数来,得到n1个数和n2个数的平均值,求这两个平均值的最大和 分析:排个序从后面抽,注意先从末尾抽个数小的,再抽个数大的 ...