Poj 1861 Kruskal

Source: Internet
Author: User
Tags integer numbers
Network
Time Limit: 1000 ms memory limit: 30000 K
Total submissions: 13456 accepted: 5209 Special Judge

Description

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 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 is not exceed 106. 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.
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

Source

Northeastern Europe 2001, northern subregion
 
/*******************************************       author   :   Grant Yuan       time     :   2014.7.31       algorithm:   Kruskal       source   :   POJ 1861       notice   :   sample is error!********************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<queue>#include<vector>#define INF 0x3fffffff#define MAX1 1003#define MAX2 15003using namespace std;queue<int>q;int par[MAX1];struct edge{int u;int v;int cost;};edge e[MAX2];int n,m,ans;int ml;bool cmp(edge aa,edge bb){    return aa.cost<bb.cost;}int findp(int aa){    if(aa==par[aa]) return aa;    else        return par[aa]=findp(par[aa]);}void unite(int aa,int bb){    int a1,b1;    a1=findp(aa);    b1=findp(bb);    par[b1]=a1;}bool same(int aa,int bb){    return findp(aa)==findp(bb);}int main(){   while(~scanf("%d%d",&n,&m)){    ans=0;ml=0;    while(!q.empty()) q.pop();    for(int i=1;i<=n;i++)    {        par[i]=i;    }    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);    }    sort(e+1,e+m+1,cmp);    for(int i=1;i<=m;i++)    {        if(!same(e[i].u,e[i].v)){            unite(e[i].u,e[i].v);            ans++;            if(e[i].cost>ml)                 ml=e[i].cost;            q.push(i);        }    }    int t;    printf("%d\n",ml);    printf("%d\n",ans);    while(!q.empty()){        t=q.front();        q.pop();        printf("%d %d\n",e[t].u,e[t].v);    }   }    return 0;}

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.