ZOJ 1542 Network

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1542

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个集线器,集线器之间用网线连接。要访问整个网络。需要找出线路中最长的单根网线的长度在所有方案中是最小的。需要输出网线数目,网线的链接编号

分析:对于一个图的最小生成树,它的最大边满足在所有生成树的最大边里面最小。

#include <bits/stdc++.h>
using namespace std;
const int N=1010;
struct node{
    int u,v,d;
}q[100*N];
int pre[N];
int n,m,maxL;
void init(){
    for(int i=1;i<=n;i++){
        pre[i]=i;
    }
}
int Find(int x){
    if(x!=pre[x]) pre[x]=Find(pre[x]);
    return pre[x];
}
void join(int x,int y){
    int tx=Find(x);
    int ty=Find(y);
    if(tx!=ty){
        pre[ty]=tx;
    }
}
bool cmp(node a,node b){
    return a.d<b.d;
}

vector<pair<int,int> > p;
void Kru(){
    maxL=0;
    p.clear();
    sort(q+1,q+1+m,cmp);
    for(int i=1;i<=m;i++){
        if(Find(q[i].u)!=Find(q[i].v)){
            join(q[i].u,q[i].v);
            p.push_back(make_pair(q[i].u,q[i].v));
            maxL=max(maxL,q[i].d);
        }
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        int a,b,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&q[i].u,&q[i].v,&q[i].d);
        }
        Kru();
        printf("%d\n%d\n",maxL,n-1);
        for(int i=0;i<p.size();i++){
            cout<<p[i].first<<" "<<p[i].second<<endl;
        }
    }

    return 0;
}
View Code

 

posted on 2018-09-26 14:54  坤sir  阅读(129)  评论(0编辑  收藏  举报

导航