// UVa 11857 - Driving Range
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct edge {
int a, b, c;
};
bool operator <(edge x, edge y) {
return x.c < y.c;
}
int C[1000000];
int find(int x) {
return (C[x] == x) ? x : C[x] = find(C[x]);
}
void join(int x, int y) {
int px = find(x);
int py = find(y);
C[py] = px;
}
vector<edge> lnk;
int main() {
while (true) {
int n, m;
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
break;
lnk.clear();
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
lnk.push_back( { a, b, c });
}
sort(lnk.begin(), lnk.end());
for (int i = 0; i < n; i++)
C[i] = i;
int sol = -1, count = 1;
for (int j = 0; j < m; j++) {
int a = lnk[j].a;
int b = lnk[j].b;
int c = lnk[j].c;
if (find(a) != find(b)) {
count++;
if (count == n) {
sol = c;
break;
}
join(a, b);
}
}
if (sol == -1)
printf("IMPOSSIBLE\n");
else
printf("%d\n", sol);
}
return 0;
}
Saturday, May 16, 2015
UVa 11857 - Driving Range
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment