LA5713 Qin Shi Huang's National Road System
题目大意:秦始皇要在n个城市之间修筑一条道路使得任意两个城市均可连通。有个道士可以用法力帮忙修一条路。秦始皇希望其他的道路总长B最短且用法术连接的两个城市的人口之和A尽量大,因此下令寻找一个A / B的最大方案。(转自http://blog.csdn.net/murmured/article/details/18865721,侵删)
题解:考虑修哪一条路,此时A确定,断掉这条路后,形成两个连通块,不难发现两个连通块都应是MST才能让B最短。此时B为两个连通块的MST权值和
这个过程相当于选了一条路u->v后,在整张图的MST上,把MST上u->v路径上最大边删掉。
其实不用枚举边,枚举u和v即可
空间开小了,RE了无数发。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
template<class T>
inline void swap(T &a, T &b)
{
T tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXM = + ; int x[MAXM], y[MAXM], node[MAXM], n, m, t, u[MAXM], v[MAXM], cnt[MAXM], fa[MAXM], vis[MAXN];
double ma[MAXN][MAXN], w[MAXM];
struct Edge
{
int u,v,nxt;
double w;
Edge(int _u, int _v, double _w, int _nxt){u = _u;v = _v;w = _w;nxt = _nxt;}
Edge(){}
}edge[MAXM << ];
int head[MAXN], cntt;
inline void insert(int a, int b, double c)
{
edge[++cntt] = Edge(a,b,c,head[a]);
head[a] = cntt;
}
int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}
int cmp(int x, int y){return w[x] < w[y];} int tiaoshi;
void dfs(int x, int pre)
{
vis[x] = ;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(v == pre) continue;
for(int i = ;i <= n;++ i)
if(i == v || !vis[i]) continue;
else ma[i][v] =ma[v][i] = max(ma[i][v], max(ma[v][i], max(ma[x][i], max(ma[i][x], edge[pos].w))));
dfs(v, x);
}
}
int main()
{
read(t);
for(;t;--t)
{
tiaoshi = ;
read(n);memset(ma, , sizeof(ma)), memset(vis, , sizeof(vis)), memset(head, , sizeof(head)), cntt = , m = ;
for(int i = ;i <= n;++ i) read(x[i]), read(y[i]), read(node[i]);
for(int i = ;i <= n;++ i)
for(int j = i + ;j <= n;++ j)
++ m, u[m] = i, v[m] = j, w[m] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])), cnt[m] = m, fa[m] = m;
std::sort(cnt + , cnt + + m, cmp);
int tmp = ;
double ans = ;
double ans2 = ;
for(int i = ;i <= m;++ i)
{
int f1 = find(u[cnt[i]]), f2 = find(v[cnt[i]]);
if(f1 == f2) continue;
fa[f1] = f2;
insert(u[cnt[i]], v[cnt[i]], w[cnt[i]]), insert(v[cnt[i]], u[cnt[i]], w[cnt[i]]);
ans += w[cnt[i]];
++ tmp;if(tmp == n - ) break;
}
dfs(, -);
for(int i = ;i <= n;++ i)
for(int j = i + ;j <= n;++j)
{
if(ans == ma[i][j]) continue;
ans2 = max(ans2, (double)(node[i] + node[j]) / (ans - ma[i][j]));
}
printf("%.2lf\n", ans2);
}
return ;
}
LA5713
LA5713 Qin Shi Huang's National Road System的更多相关文章
- 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 ...
- UValive 5713 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 ...
- 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 ...
- 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 ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- 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 ...
- 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 ...
随机推荐
- 面试系列八 es写入数据的工作原理
(1)es写数据过程 1)客户端选择一个node发送请求过去,这个node就是coordinating node(协调节点) 2)coordinating node,对document进行路由,将请求 ...
- nvelocity的Foreach 中使用DataTable数据
原文:nvelocity的Foreach 中使用DataTable数据 tripDetailList是一个DataTable类型的数据,Logo.TripTypeName.TipTypePrice等为 ...
- 如何在安装了Owin 2.X版本的项目中正确安装SignalR 2.2.1
以安装了Owin2.1.0版本为例 1打开NuGet程序包管理控制台 2输入 Install-Package Microsoft.AspNet.SignalR -Version 2.2.1 2输入 I ...
- 锋利的Jquery(点击显示隐藏div)
点击显示隐藏div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- IDA*算法——骑士精神
例题 骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者 ...
- WebLogic使用总结(二)——WebLogic卸载[转]
一.WebLogic 12c的卸载 WebLogic的卸载是非常容易的,找到WebLogic的卸载程序,如下图所示:
- Windows的SEH机理简要介绍
1.异常分类 一般来说,我们把Exception分为2类,一类是CPU产生的异常,我们称之为CPU异常(或者硬件异常).另一类为是通过调用RaiseException API产生的软件异常,我们称之为 ...
- requirements.txt 作用
requirements.txt的作用 用于记录所有依赖包及其精确的版本号.以便新环境部署. 使用pip生成 pip freeze >requirements.txt 当需要创建这个虚拟环境的完 ...
- 使用Log4net把日志写入到SqlServer数据库
1.官网URL: http://logging.apache.org/log4net/ 2.配置文件参照URL: http://logging.apache.org/log4net/release/c ...
- 关于bind
1, 目的 使程序可以感知到事件 2, 格式 widget.bind(event, handler) 3, handler注意事项 在这里, handler作为一个函数, 是需要一个event对象作为 ...