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 ...
随机推荐
- 02_mybatis开发dao的方法
MyBatis开发dao的方法 1. SqlSession使用范围 1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂Sql ...
- css中字体属性的简写
- 使用github作为远程仓库的常见git操作
[git上传本地代码到github新建仓库]一.建立git本地仓库 1.在本地目标文件夹(Code)中执行命令: git init //初始化本地仓库二.将上传到github的项目文件添加到本地仓库中 ...
- python 简单的图片比较
# by movie on 2019/12/18 from PIL import Image from PIL import ImageChops path1 = 'images/trumpA689. ...
- 设置IDEA中properties文件显示中文
路径: File - Setting - Editor - Code Style - File Encodings
- Type.GetType(string.contains(','))
例如 Type type = Type.GetType("ACalCoreServiceLib.BaseService,ACalCoreServiceLib"); 里面的ACalC ...
- 提高Modelsim仿真速度的方法(1) -- force
假如主驱动时钟频率很高,因为要一个周期输出,仿真时间过长,仿真速度慢是自然. 但是仿真中,并不是每个驱动周期都是必要的,这时可以使用force命令把想要的信号提前制造出来. 事实上,对于使用到PLL的 ...
- 学习笔记 css样式
大小 width:宽度 height:高度 背景 background-color 背景色 background-image 背景图片 background-repeat 背景平铺 ...
- SG函数博弈——poj2311
关于SG函数的博弈 首先定义必败态 x : SG[x]=0 设任意一个状态y,到所有y能到达的状态连一条边,令这些后继为z y : SG[y]=mex(SG[z]) SG[y]==0 : y就是必败态 ...
- 用C++Builder在Windows开始按钮上绘图制作方法
熟悉Windows操作系统的软件设计人员知道,在Win95/98/NT/2000中有一任务栏(Task Bar)程序,路径为:C:\WINDOWS\SYSTEM\SYSTRAY.EXE(假设你的Win ...