原题链接

pdf

题目大意

给出一张无向图图,求该图的最小瓶颈生成树。

无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的。瓶颈生成树的值为\(T\)中最大权值边的权。

该图建立在坐标系中, 给出每个点的坐标。任意两点之间都有边,边权即为两点间的距离。

题解

由于只关心生成树的最大值,我们可以将边从小到大排序,依次加入(若构成环则不加入),直到构成一颗生成树。

相信你已经发现了:这不就是Kruskal算法吗?

于是,我们得出结论:无向图的最小生成树一定是瓶颈生成树。

如果你仍然感到怀疑,那么我们再用反证法证明:

假设存在一张无向图的最小生成树\(T\)不是瓶颈生成树,那么我们找到该最小生成树的权值最大边\(e\),我们选取该图中的一颗瓶颈生成树\(T_1\),则有:对于\(T_1\)中的任何边\(e_1\),存在\(V_{e_1} <V_{e}\)。删除\(T\)中的\(e\),我们得到两棵树\(T_a,T_b\)。由于\(T_1\)是一颗生成树,必有一条边\(e_{ab}\)连接\(T_a,T_b\),用\(e_{ab}\)替换\(e\),可以得到更小的生成树,与\(T\)是最小生成树矛盾。证毕。

顺便提一句,无向图瓶颈生成树一定是最小生成树吗?

看一看下图就知道了:

由于本题是稠密图,最好用Prim解决(然而懒到家的我还是用了Kruskal)。

听说有一种复杂度更优的算法叫Camerini's algorithm(然而我并不会),如果有大神会的话也可以教导我一下。

代码

#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int maxn = 5005; struct City
{
double x, y;//注意是小数(开float似乎也行)
} city[maxn]; struct Edge
{
int from, to;
double dist; bool operator < (const Edge& other) const
{
return dist < other.dist;
}
} edge[maxn*maxn]; int n, m, S; inline double sqr(double a)
{
return a*a;
} inline double make_dist(City a, City b)
{
return sqrt(sqr(a.x-b.x) + sqr(a.y-b.y));
} inline void add_edge(City a, City b, int ai, int bi)
{
double dist = make_dist(a, b);
m++;
edge[m].from = ai;
edge[m].to = bi;
edge[m].dist = dist;
} inline void read()
{
scanf("%d%d", &S, &n);
S = n-S;
for(int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &city[i].x, &city[i].y);
for(int j = 1; j < i; ++j)
add_edge(city[i], city[j], i, j);
}
} struct UN_set
{
int fa[maxn]; inline void init(int n)
{
for(int i = 1; i <= n; ++i)
fa[i] = i;
} inline int getfa(int x)
{
return fa[x] == x ? x : fa[x] = getfa(fa[x]);
}
} un; inline double Kruskal()//其实最好还是用prim
{
int tmp = 0;
m = 0;
read();
sort(edge+1, edge+m+1);
un.init(n);
for(int i = 1; i <= m; ++i)
{
int ff = un.getfa(edge[i].from);
int tf = un.getfa(edge[i].to);
if(ff != tf)
{
un.fa[ff] = tf;
tmp++;
if(tmp == S)
return edge[i].dist;
}
}
return -1;
} int main()
{
int nnn;
scanf("%d", &nnn);
while(nnn--)
printf("%.2f\n", Kruskal());//直接求最小生成树即可
return 0;
}

题解:UVa1025 A Spy in the Metro的更多相关文章

  1. UVA1025 A Spy in the Metro —— DP

    题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...

  2. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  3. Uva1025 A Spy in the Metro

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...

  4. 【Uva1025 A Spy in the Metro】动态规划

    题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...

  5. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  6. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  7. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  8. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

  9. uva 1025 A Spy int the Metro

    https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...

随机推荐

  1. odoo 币别符号显示机制 Monetary

    //-------------------------------------------------------------------basic_fields.js init: function ...

  2. 【C++】虚函数的实现机制

    一.什么是虚函数? 虚函数是在类中由virtual关键字声明的成员函数,并且每一个含有虚函数的类都至少有一个与之对应的虚函数表,其中存放着该类所有虚函数对应的函数指针 在基类中进行如下定义: virt ...

  3. [转帖]AWR报告参数:DB TIME和DB CPU

    AWR报告参数:DB TIME和DB CPU http://blog.itpub.net/12679300/viewspace-1182396/ 一.前言:AWR报告是了解ORACLE运行的一个重要报 ...

  4. 排行榜 和 zset

    ZSET 使用 https://blog.csdn.net/weixin_37490221/article/details/78135036 https://www.cnblogs.com/chenz ...

  5. 深入理解 Linux Cgroup 系列(一):基本概念

    原文链接:深入理解 Linux Cgroup 系列(一):基本概念 Cgroup 是 Linux kernel 的一项功能:它是在一个系统中运行的层级制进程组,你可对其进行资源分配(如 CPU 时间. ...

  6. 『Blocks 区间dp』

    Blocks Description Some of you may have played a game called 'Blocks'. There are n blocks in a row, ...

  7. Python语言的特点及自学建议

    Python语言的特点Python语言是一种被广泛使用的高级通用脚本编程语言,具有很多区别于其他语言的特点,这里仅列出如下一些重要特点.(1)语法简洁:实现相同功能,Python语言的代码行数仅相当于 ...

  8. SpringMVC+EasyUI实现页面左侧导航菜单

    1. 效果图展示 2. 工程目录结构 注意: webapp下的resources目录放置easyui和js(jQuery文件是另外的)                    3. 代码 index.j ...

  9. Python进阶----索引原理,mysql常见的索引,索引的使用,索引的优化,不能命中索引的情况,explain执行计划,慢查询和慢日志, 多表联查优化

    Python进阶----索引原理,mysql常见的索引,索引的使用,索引的优化,不能命中索引的情况,explain执行计划,慢查询和慢日志, 多表联查优化 一丶索引原理 什么是索引:       索引 ...

  10. axios + vue导出excel文件

    (使用到了elementUI框架) <template> <el-button type="primary" size="mini" @cli ...