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;
} 

posted @   feuerwerk  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示