题目大意:秦始皇要在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的更多相关文章

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

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

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

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

  5. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

    Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...

  6. [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 ...

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

    Qin Shi Huang's National Road System                                                                 ...

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

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

随机推荐

  1. Python对接支付宝支付自实现

    Python对接支付宝支付自实现 # -*- coding: utf-8 -*- import base64 import json import urllib.parse from datetime ...

  2. Python第二课-输入输出

    name = input() 输入的字符串已经赋值给变量name print() 输出内容 print(,) print中,连接字符串相当于空格

  3. html自定义分页

    public class MyPager { /// <summary> /// 每一页数据的条数 /// </summary> public int PageSize { g ...

  4. SUMMARY | 二分查找

    package Search; public class biSearch { //标准的二分查找 public static int stdBiSearch(int[] array,int keyV ...

  5. Ubuntu环境下Error: Invalid or corrupt jarfile xxx.jar

    一.问题描述 Ubuntu环境下将Maven项目打包成jar包后,运行一下指令: $ java -jar my.jar 发生错误: Error: Invalid or corrupt jarfile ...

  6. xml的修改遍历,以及建立

    1.xml的遍历 2.xml的遍历 3.xml的建立

  7. SQLServer索引的四个高级特性

    一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引   SQLSer ...

  8. swoole是如何实现任务定时自动化调度的?

    https://www.muzilong.cn/article/117 开发环境 环境:lnmp下进行试验. 框架:laravel5 问题描述 这几天做银行对帐接口时,踩了一个坑,具体需求大致描述一下 ...

  9. 关于获取webview(窗口间关系)的方法

    1.获取指定页面ID的webview plus.webview.getWebviewById('为页面设置的id值'): 该方法主要用于首页底部导航切换到子页面时不执行子页面的函数,因为在设置导航的时 ...

  10. Python学习之while练习--九九乘法表

    效果如下: 实现代码; m = 1n = 1while(m<10): while(n<=m): print(n,"*",m,"=",m*n,end ...