POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】

Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).

Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem - not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.

You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.


Input

The first line of the input file contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 10^6. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.

Process to the end of file.


Output

Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.


Sample Input

4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1


Sample Output

1
4
1 2
1 3
2 3
3 4


题目大意:给n个点,m条边。求一棵生成树,并且满足任意两点之间的距离的最大值最小。输出这个最大值,然后输出树的边的数量,最后输出树的每条边。

题目思路:对于一个生成树来说,它的最大边满足在所有的生成树的最大边里最小,这其实就是一颗最小生成树。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1010
#define maxm 20000
using namespace std;

struct node{
    int u, v, w;
};

node edge[maxm];
int per[maxn];
int n, m;
int ans[maxn];//记录依次选择边的序号
int maxedge, k, num;

int cmp(node a, node b){
    return a.w < b.w;
}

void init(){
    for(int i = 1; i <= n; ++i)
        per[i] = i;
}

int find(int x){
    if(x == per[x])
        return x;
    return per[x] = find(per[x]);
}

void kruskal(){
    for(int i = 1; i <= m; ++i){
        int u = find(edge[i].u);
        int v = find(edge[i].v);
        if(u != v){
            per[u] = v;
            ans[k++] = i;
            num++;
            if(maxedge < edge[i].w)
                maxedge = edge[i].w;
        }
        if(num >= n - 1) break;
    }
}

int main (){
    while(scanf("%d%d", &n, &m) != EOF){
    for(int i = 1; i <= m; ++i)
        scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
    sort(edge + 1, edge + 1 + m, cmp);
    init();
    num = k = maxedge = 0;
    kruskal();
    printf("%d\n", maxedge);
    printf("%d\n", num);
    for(int i = 0; i < num; ++i)
        printf("%d %d\n", edge[ans[i]].u, edge[ans[i]].v);
    }
    return 0;
}


你可能感兴趣的:(POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】)