题解:UVa1025 A Spy in the Metro
题目大意
给出一张无向图图,求该图的最小瓶颈生成树。
无向图的瓶颈生成树:无向图\(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的更多相关文章
- UVA1025 A Spy in the Metro —— DP
题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- Uva1025 A Spy in the Metro
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...
- 【Uva1025 A Spy in the Metro】动态规划
题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...
- 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 ...
- UVA1025-A Spy in the Metro(动态规划)
Problem UVA1025-A Spy in the Metro Accept: 713 Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
随机推荐
- cad.net IExtensionApplication接口的妙用 分开写"启动运行"函数
cad提供的 IExtensionApplication 接口 是不能实现一次以上的,那么这给编写代码带来了一种不好的情况是,每次都要去修改实现这个接口的类, 如果是一个小的测试功能,你又要去动前面的 ...
- StringToKenizer和Scanner的区别
相同点: StringToKenizer类和Scanner类都可用于分解字符序列中的单词! 不同点: StringToKenizer类把分解出的全部字符串都存放到StringToKenizer对象的实 ...
- C/C++ 指针常量和常量指针
为了区分是指向常量的指针还是const指针(表示指针本身是常量) 一个简便方法:从由往左读,遇到p就替换为“p is a”,遇到*就替换为“point to”,其余不变. const int * p ...
- (7)ASP.NET Core 中的错误处理
1.前言 ASP.NET Core处理错误环境区分为两种:开发环境和非开发环境.●开发环境:开发人员异常页.●非开发环境:异常处理程序页.状态代码页.在Startup.Configure方法里面我们会 ...
- 服务器推送之SSE简单使用
前端 <!DOCTYPE html> <html> <head> <meta name="viewport" content=" ...
- [Windows] - Windows/Office纯绿色一键激活工具及方法
瘟到死网上有很多一件键激活工具(如KMS),但许多带毒或报毒.这里给出一个纯绿色命令行一键激活,及自已搭建激活服务器的方法. KMS现在算法都是公开的了,可以自行在网上找到,这里不详述. 使用命令行一 ...
- Elasticsearch DSL 常用语法介绍
课程环境 CentOS 7.3 x64 JDK 版本:1.8(最低要求),主推:JDK 1.8.0_121 Elasticsearch 版本:5.2.0 相关软件包百度云下载地址(密码:0yzd):h ...
- 组件切换方式(Vue.js)
这里,我用一个注册登录两组件的切换实例来演示: 切换方式一 <!DOCTYPE html> <html lang="zh-CN"> <head> ...
- XmlDocument.load 读文件报异常:文件正被其它线程使用,解决方法
string filePath = Form1.getProjectFilePath(); System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocu ...
- xenserver增加新硬盘
1. 查看所有硬盘了的id # ls -l /dev/disk/by-id/ 2. 挂载 # xe sr-create type=lvm content-type=user device-config ...