P1396 营救
哎呦喂!!看错题目啦!!还以为要用最短路,
就写了个dijkstra,呜呜呜,我就说怎么不对,
呜呜呜QAQ!!!
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 7;
int f[N];
struct Edge{
int s, t, w;
}a[N * 2];
bool cmp(Edge x, Edge y) { return x.w < y.w; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main(){
int n, m, s, t;
scanf("%d%d%d%d", &n, &m, &s, &t);
for(int i = 0;i <= n;i ++) f[i] = i;
for(int i = 1;i <= m;i ++)
scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].w);
sort(a + 1, a + 1 + m, cmp);
for(int i = 1;i <= m;i ++){
int fx = find(a[i].s), fy = find(a[i].t);
if(fx != fy) f[fx] = fy;
if(find(s) == find(t)){
printf("%d", a[i].w);
return 0;
}
}
printf("-1");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】