题解:CF2117G Omg Graph
题意
给定一张
思路
先观察题目,发现题目要求的路径不一定是简单路径,因此我们可以想到,如果路径上的最大值固定,那么最小值越小越好。因此,我们可以按边权从小到大加入边,这样我们就可以认为节点
AC记录
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, fa[N], siz[N], op[N];
struct tp{
int u, v, w;
};
vector<tp> v;
bool cmp(const tp &i, const tp &j){
return i.w < j.w;
}
int Find(int x){
return (fa[x] == x ? x : fa[x] = Find(fa[x]));
}
void Merge(int x, int y, int w){
int fx = Find(x), fy = Find(y);
if(fx != fy){
if(siz[fx] < siz[fy]){
swap(fx, fy);
}
fa[fy] = fx;
siz[fx] += siz[fy];
op[fx] = min({op[fx], op[fy], w});
}
}
void solve(){
cin >> n >> m;
v.clear();
for(int i = 1, u, V, w; i <= m; i++){
cin >> u >> V >> w;
v.push_back({u, V, w});
}
fill(siz + 1, siz + n + 1, 1);
iota(fa + 1, fa + n + 1, 1);
fill(op + 1, op + n + 1, 2e9 + 5);
sort(v.begin(), v.end(), cmp);
int ans = 2e9;
for(tp it : v){
Merge(it.u, it.v, it.w);
if(Find(1) == Find(n)){
ans = min(ans, it.w + op[Find(1)]);
}
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}