These days, many carmakers are developing cars that run on electricityinstead of gasoline. The batteries used in these cars are generallyvery heavy and expensive, so designers must make an important tradeoffswhen determining the battery capacity, and therefore the range, of thesevehicles. Your task is to help determine the minimum range necessaryso that it is possible for the car to travel between any two cities onthe continent.
The road network on the continent consists of cities connected bybidirectional roads of different lengths. Each city contains a charging station.Along a route between two cities, the car may pass through anynumber of cities, but the distance between each pair of consecutivecities along the route must be no longer than the range of the car.What is the minimum range of the car so that there is a routesatisfying this constraint between every pair of cities on the continent?
Input Specification
The input consists of a sequence of road networks.The first line of each road network contains two positive integers n and m, the number of cities and roads.Each of these integers is no larger than one million.The cities are numbered from 0 to n-1.The first line is followed by m more lines,each describing a road. Each such line contains three non-negativeintegers. The first two integers are the numbers of the two citiesconnected by the road. The third integer is the length of the road.The last road network is followed by a line containing two zeros,indicating the end of the input.Sample Input
3 3 0 1 3 1 2 4 2 1 5 2 0 0 0
Output Specification
For each road network, output a line containing one integer,the minimum range of the car that enables it to drive from everycity to every other city. If it is not possible to drive fromsome city to some other city regardless of the range of thecar, instead output a line containing the word IMPOSSIBLE.Output for Sample Input
4 IMPOSSIBLE题意:给出n个点及m个路径,求构成的图连通的最大的边的值
思路:Kruskal算法,先将边排序,然后用并查集。如果并查集的集合的个数与n相等,说明是可行的
#include <cstdio>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
class UnionFindSet
{
public:
UnionFindSet(int size = 0);
int find(int a);
bool isSameSet(int a, int b);
void unionSet(int a, int b);
int numDisjointSets();
int sizeOfSet(int a);
void setSize(int n);
void init(int size);
private:
int size;
vector<int> pset;
vector<int> set_size;
};
void UnionFindSet::init(int size)
{
this->size = size;
pset.assign(size, 0);
set_size.assign(size, 1);
for (int i = 0; i < size; i++) {
pset[i] = i;
}
}
UnionFindSet::UnionFindSet(int size)
{
init(size);
}
int UnionFindSet::find(int a)
{
return pset[a] == a ? a : (pset[a] = find(pset[a]));
}
bool UnionFindSet::isSameSet(int a, int b)
{
return find(a) == find(b);
}
void UnionFindSet::unionSet(int a, int b)
{
if (isSameSet(a, b)) return;
size--;
set_size[find(a)] += set_size[find(b)];
pset[find(b)] = pset[find(a)];
}
int UnionFindSet::numDisjointSets()
{
return size;
}
int UnionFindSet::sizeOfSet(int a)
{
return set_size[find(a)];
}
void UnionFindSet::setSize(int n)
{
init(n);
}
int n, m;
UnionFindSet unionSet;
bool input()
{
scanf("%d%d", &n, &m);
if (n == 0 && m == 0) return false;
unionSet.setSize(n);
return true;
}
void solve()
{
int a, b, c;
priority_queue< pair<int, pair<int, int> > > edgeList;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
edgeList.push(make_pair(-c, make_pair(a, b)));
}
int ans = 0;
while (!edgeList.empty()) {
pair<int, pair<int, int> > edge = edgeList.top(); edgeList.pop();
if (!unionSet.isSameSet(edge.second.first, edge.second.second)) {
if (-edge.first > ans) {
ans = -edge.first;
}
unionSet.unionSet(edge.second.first, edge.second.second);
}
}
if (unionSet.sizeOfSet(0) != n) {
printf("IMPOSSIBLE\n");
} else {
printf("%d\n", ans);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
while (input()) {
solve();
}
return 0;
}