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 ...
随机推荐
- 高仿ios版美团框架项目源码
高仿美团框架基本已搭好.代码简单易懂,适合新人.适合新人.新人. <ignore_js_op> 源码你可以到ios教程网那里下载吧,这里我就不上传了,http://ios.662p ...
- iOS7之后设置NavigationBar的背景
iOS7之后,请注意需要使用setBarTintColor ``` [self.navigationController.navigationBar setBarTintColor:[UIColor ...
- although 和 although 的区别
作为连词的时候,although 和 though 是可以互换的.Although 一般被认为更加正式一些.比如,以下的这些句子: Growth in Europe is maintaining mo ...
- 数据库实战案例—————记一次TempDB暴增的问题排查
前言 很多时候数据库的TempDB.日志等文件的暴增可能导致磁盘空间被占满,如果日常配置不到位,往往会导致数据库故障,业务被迫中断. 这种文件暴增很难排查,经验不足的一些运维人员可能更是无法排查具体原 ...
- ASP.NET Cookie(二)--控制Cookie的范围
默认情况下,一个站点的全部Cookie都一起存储在客户端上,而且所有Cookie都会随着对该站点发送的任何请求一起发送到服务器.也就是说,一个站点中的每个页面都能获得该站点的所有Cookie.但是,可 ...
- 基于easyUI实现组织结构树图形
一. 准备工作 1. 点击此下载相关文件 2. 进入 js 文件夹,解压缩 jquery-easyui-1.5.rar 到当前文件夹 二. 在浏览器中运行 organize.html 文件,即可看到效 ...
- 如何利用报表工具FineReport实现报表列的动态展示
相信动态列的实现困扰了很多人,大数据量,多字段的加载将会非常耗时,数据又做不到真正的动态灵活.现有的方式都是通过变向的隐藏等方式来实现. 那该如何解决呢?这里分享帆软报表设计器FineReport的实 ...
- L1-006. 连续因子
https://www.patest.cn/contests/gplt/L1-006 题目地址 在上面 一个正整数N的因子中可能存在若干连续的数字.例如630可以分解为3*5*6*7,其中5.6.7就 ...
- NGUI裁剪模型和粒子
效果预览 注:Cube上附着的绿色是我添加的粒子效果. 软件环境 NGUI 3.9.x Unity 5.1 x64 相关知识 RenderTexture RenderTexture是一种特殊的纹理,它 ...
- TimerToPdf
1.----------------TimerEvent-------------------var today:Date=new Date();hours == today.hours;minute ...