【luogu P1396】营救
https://www.luogu.org/problem/show?pid=1396
弱化版的货车运输,用并查集维护连通块,将边按权值升序排序后依次插入直到两点连通,最后插入的边的权值就是最小的拥挤度最大值。
#include <iostream>
#include <vector>
#include <algorithm>
#define maxn 100005
using namespace std;
int n, m, s, t;
struct edge
{
int from, to, weight;
};
bool operator<(const edge &x, const edge &y)
{
return x.weight < y.weight;
}
vector<edge> e;
namespace djs
{
int parent[maxn];
void init()
{
for (int i = ; i <= n; i++)
parent[i] = -;
}
int find(int x)
{
if (parent[x] < )
return x;
else
return parent[x] = find(parent[x]);
}
void merge(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (parent[x] > parent[y])
swap(x, y);
parent[x] += parent[y];
parent[y] = x;
}
bool related(int x, int y)
{
return find(x) == find(y);
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> s >> t;
int a, b, c;
for (int i = ; i < m; i++)
{
cin >> a >> b >> c;
e.push_back((edge){a, b, c});
}
sort(e.begin(), e.end());
djs::init();
int ans = ;
for (int i = ; i < e.size() && !djs::related(s, t); i++)
{
djs::merge(e[i].from, e[i].to);
ans = e[i].weight;
}
cout << ans << endl;
return ;
}
【luogu P1396】营救的更多相关文章
- luogu P1396 营救
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- Luogu P1396 营救【最小生成树/二分答案/最短路】 By celur925
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- 洛谷 P1396 营救
题目链接 https://www.luogu.org/problemnew/show/P1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪 ...
- P1396 营救
P1396 营救 218 通过 571 提交 题目提供者yeszy 标签 二分 图论 并查集 福建省历届夏令营 难度 普及- 题目描述 "咚咚咚--""查水表!" ...
- 洛谷——P1396 营救
P1396 营救 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈 ...
- 洛谷P1396 营救 题解
题目:https://www.luogu.org/problemnew/show/P1396 分析: 这其实一看就是一个最短路的近似模板的题目,但我们要注意到两个区之间可能会有多条道路,所以说我们只需 ...
- P1396 营救 洛谷
https://www.luogu.org/problem/show?pid=1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起 ...
- P1396 营救(并查集+二分)
思路:检验函数中,先初始化每个节点的下标,每调用检验函数就从新使用一次并查集(并查集的时间复杂度非常低),然后,就看当一条路的价值val<=假设最大值x时,就把他们连接起来. #include& ...
- [P1396]营救 (并查集)
大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...
随机推荐
- A strange lift
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- vue小项目---管理系统
在上一篇文章中我们已经学习了vue的基本语法,常用属性,了解了vue的基本使用,现在让我们用vue配合Bootstrap来完成一个小项目. 首先导入Bootstap文件. <link rel=& ...
- IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)
之前写了一篇文章:<IdentityServer4 实现 OpenID Connect 和 OAuth 2.0> 上面这篇文章虽然详细,但都是点到为止的介绍,并没有实际应用的示例,所以,后 ...
- 初入servlet:Allocate exception for servlet
老板,来一碗错误垫垫肚子! 如果以下几个错误都符合,估计就是这个原因了. 页面报错如下: java.lang.NoClassDefFoundError:IllegalName: firstDemo/T ...
- Windows 配置 allure report 环境
1:配置Java环境(运行allure 需要) 2:安装powershell 3:安装scoop方法 :运行 powershell 输入 : iex (new-object net.webclient ...
- mysql查询锁表及解锁
SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...
- winscp连接虚拟机Linux被拒绝的问题解决方案
输入了正确的账号密码还出现这个错误 我们需要在虚拟机中配置一下,改成这样就行了
- 【OpenCV】通过ROI区域以及掩码实现图像叠加
在图像处理领域,我们常常需要设置感兴趣区域(ROI,region of interest),来专注或者简化我们的工作过程 .也就是从图像中选择的一个图像区域,这个区域是我们图像分析所关注的重点.我们圈 ...
- Java实现邮箱发送验证码
第一步,导入JAR包,JAR包下载地址[http://pan.baidu.com/s/1kVRvGyF] 正式代码: 首先书写一个工具类: MailUtil import javax.mail.*; ...
- URL模块之parse方法
url.parse(urlString , boolean , boolean) parse这个方法可以将一个url的字符串解析并返回一个url的对象. 参数: urlString指传入一个url地址 ...