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 = 1; i <= n; i++) parent[i] = -1; } int find(int x) { if (parent[x] < 0) 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 = 0; i < m; i++) { cin >> a >> b >> c; e.push_back((edge){a, b, c}); } sort(e.begin(), e.end()); djs::init(); int ans = 0; for (int i = 0; i < e.size() && !djs::related(s, t); i++) { djs::merge(e[i].from, e[i].to); ans = e[i].weight; } cout << ans << endl; return 0; }
时间: 2024-10-18 01:02:50