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】营救的更多相关文章

  1. luogu P1396 营救

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  2. Luogu P1396 营救【最小生成树/二分答案/最短路】 By celur925

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  3. 洛谷 P1396 营救

    题目链接 https://www.luogu.org/problemnew/show/P1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪 ...

  4. P1396 营救

    P1396 营救 218 通过 571 提交 题目提供者yeszy 标签 二分 图论 并查集 福建省历届夏令营 难度 普及- 题目描述 "咚咚咚--""查水表!" ...

  5. 洛谷——P1396 营救

    P1396 营救 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈 ...

  6. 洛谷P1396 营救 题解

    题目:https://www.luogu.org/problemnew/show/P1396 分析: 这其实一看就是一个最短路的近似模板的题目,但我们要注意到两个区之间可能会有多条道路,所以说我们只需 ...

  7. P1396 营救 洛谷

    https://www.luogu.org/problem/show?pid=1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起 ...

  8. P1396 营救(并查集+二分)

    思路:检验函数中,先初始化每个节点的下标,每调用检验函数就从新使用一次并查集(并查集的时间复杂度非常低),然后,就看当一条路的价值val<=假设最大值x时,就把他们连接起来. #include& ...

  9. [P1396]营救 (并查集)

    大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...

随机推荐

  1. Problem F

    Problem Description "Yakexi, this is the best age!" Dong MW works hard and get high pay, h ...

  2. js里面的map、filter、forEach、reduce、for in、for of等遍历方法

    1.map 遍历数组,改变原数组 [2, 3, 4, 5].map(function(val, key,arr) { return val > 3; }) var data = [ { name ...

  3. 学习C++ 模板类

    #include<iostream>#include<typeinfo>#include<cstring> using namespace std; class A ...

  4. express的学习,与使用

    最近在学习vue的一个实战项目,碰到一个express,当时很萌,就随便看了看................ expres是基于node 的一个web框架, 首先可以找到它的官网照着学习 这里只讲一 ...

  5. 如何编写通用的 Helper Class

    Github: https://github.com/nzbin/snack-helper Docs: https://nzbin.github.io/snack-helper 前言 什么是 help ...

  6. MyBatis学习笔记1--初识MyBatis

    我也是初学者,写博客只是想把自己的整个思路整理一下,有不对或者不好的地方,请大家多多指正. 1.MyBatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...

  7. Python之numpy模块array简短学习

    1.简介 Python的lists是非常的灵活以及易于使用.但是在处理科学计算相关大数量的时候,有点显得捉襟见肘了. Numpy提供一个强大的N维数组对象(ndarray),包含一些列同类型的元素,这 ...

  8. 阿里Dubbo疯狂更新,关Spring Cloud什么事?

    最近,开源社区发生了一件大事,那个全国 Java 开发者使用最广的开源服务框架 Dubbo 低调重启维护,并且 3 个月连续发布了 4 个维护版本. 我上次在写放弃Dubbo,选择最流行的Spring ...

  9. 利用 WSL 在 Windows下打造高效的 Linux 开发环境

    WSL-Windows Subsystem for Linux 介绍 The Windows Subsystem for Linux lets developers run Linux environ ...

  10. HashMap HashTable和ConcurrentHashMap的区别

    HashMap和Hashtable都实现了Map接口,其主要的区别有:线程安全性,同步(synchronization),以及效率. HashMap和Hashtable基本上没啥区别,除了HashMa ...