UVA11857 HDU3742 Driving Range【最小生成树+Kruskal算法】

These days, many carmakers are developing cars that run on electricity instead of gasoline. The batteries used in these cars are generally very heavy and expensive, so designers must make an important tradeoffs when determining the battery capacity, and therefore the range, of these vehicles. Your task is to help determine the minimum range necessary so that it is possible for the car to travel between any two cities on the continent.

    The road network on the continent consists of cities connected by bidirectional roads of different lengths. Each city contains a charging station. Along a route between two cities, the car may pass through any number of cities, but the distance between each pair of consecutive cities 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 route satisfying this constraint between every pair of cities on the continent?
Input
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-negative integers. The first two integers are the numbers of the two cities connected 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.
Output
For each road network, output a line containing one integer, the minimum range of the car that enables it to drive from every city to every other city. If it is not possible to drive from some city to some other city regardless of the range of the car, instead output a line containing the word ‘IMPOSSIBLE’.
Sample Input
3 3
0 1 3
1 2 4
2 1 5
2 0
0 0
Sample Output
4
IMPOSSIBLE

问题链接UVA11857 HDU3742 Driving Range
问题简述:(略)
问题分析:简单题,不解释看代码,用Kruskal算法解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11857 HDU3742 Driving Range */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000000;
struct Edge { int u, v, w;} e[N];
bool cmp ( Edge a, Edge b ) {  return a.w < b.w; }
int f[N + 1], fcnt;
void UFInit(int n) {for(int i = 0; i <= n; i++) f[i] = i; fcnt = n;}
int Find(int a) {return a == f[a] ? a : f[a] = Find(f[a]);}

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m) && (n || m)) {
        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);

        UFInit(n);

        // Kruskal算法
        sort(e, e + m, cmp);
        int ans;
        for(int i = 0; i < m; i++) {
            int u = Find(e[i].u);
            int v = Find(e[i].v);
            if ( u != v ) {
                f[u] = v;
                ans = e[i].w;
                if(--fcnt == 1) break;
            }
        }

        if(fcnt == 1) printf("%d\n", ans);
        else printf("IMPOSSIBLE\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值