ZOJ 1542 Network

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 阅读(...) 评论(...) 编辑 收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值