//新生训练
Input
3 3 1 3
1 2 2
2 3 1
1 3 3
Output
2
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
const int N = 20020;
int n, m, s, t;
struct role
{
int x, y, z;
} e[N];
int f[N];
void init()
{
for (int i = 1; i <= n; i++)
f[i] = i;
}
int find(int x)
{
if (f[x] == x)
return x;
return f[x] = find(f[x]);
}
void merge(int a, int b)
{
if (find(a) != find(b))
f[a] = find(f[b]);
}
bool cmp(role a, role b)
{
return a.z < b.z;
}
int main()
{
cin >> n >> m >> s >> t;
init();
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
}
sort(e + 1, e + 1 + m, cmp);
for (int i = 1; i <= m; i++)
{
int x = find(e[i].x), y = find(e[i].y);
if (x != y)
merge(x, y);
if (find(s) == find(t))
{
printf("%d", e[i].z);
break;
}
}
return 0;
}
~~~仅当笔者个人备忘录使用。