#include <iostream>
#include <algorithm>
using namespace std;
int s[2000];
struct Map
{
int x, y, t;
}map[100000];
bool cmp(Map& x, Map& y)
{
return x.t < y.t;
}
int find(int x)
{
if (s[x] == x) return x;
s[x] = find(s[x]);
return s[x];
}
int main()
{
int n, m, time, j;
cin >> n >> m;
for (int i = 1; i <= n; i++)
s[i] = i;
for (int i = 1; i <= m; i++)
cin >> map[i].x >> map[i].y >> map[i].t;
sort(map + 1, map + m + 1, cmp);
for (int i = 1; i <= m; i++)
{
int root = find(map[i].x);
s[find(map[i].y)] = root;
time = map[i].t;
for (j = 2; j <= n; j++)
{
if (root != find(j)) break;
}
if (j > n)
{
cout << time << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
}
洛谷P1111 修复公路
最新推荐文章于 2024-05-02 13:33:49 发布