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 @ 2022-10-30 22:24  feuerwerk  阅读(19)  评论(0)    收藏  举报