POJ 1861 Network (Kruskal for MST template)

Source: Internet
Author: User
Tags integer numbers

POJ 1861 Network (Kruskal for MST template)

 

Network
Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:14103   Accepted:5528   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 61 2 11 3 11 4 22 3 13 4 12 4 1

Sample Output

141 21 32 33 4

Source

Northeastern Europe 2001, Northern Subregion

Link: poj.org/problem? Id = 1861

N points, m lines, each line has a weight. Now the longest path is required to be shortest and all points are connected.

Question Analysis: if there is a problem in the example, it should be
1
4
1 2
1 3
3 4
The bare Kruskal requires the shortest path, while Kruskal is a greedy algorithm for sorting weights from small to large.

#include 
 
  #include 
  
   #include using namespace std;int const MAX = 15005;int fa[MAX];int n, m, ma, num;int re1[MAX], re2[MAX]; struct Edge{    int u, v, w;}e[MAX];bool cmp(Edge a, Edge b){    return a.w < b.w;}void UF_set(){    for(int i = 0; i < MAX; i++)        fa[i] = i;}int Find(int x){    return x == fa[x] ? x : fa[x] = Find(fa[x]);}void Union(int a, int b){    int r1 = Find(a);    int r2 = Find(b);    if(r1 != r2)        fa[r2] = r1;}void Kruskal(){    UF_set();    for(int i = 0; i < m; i++)    {        int u = e[i].u;        int v = e[i].v;        if(Find(u) != Find(v))        {            re1[num] = u;            re2[num] = v;            Union(u, v);            ma = max(ma, e[i].w);            num ++;        }        if(num >= n - 1)            break;    }}int main(){      ma = 0;    num = 0;    scanf("%d %d", &n, &m);    for(int i = 0; i < m; i++)        scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);    sort(e, e + m, cmp);    Kruskal();    printf("%d\n%d\n", ma, num);    for(int i = 0; i < num; i++)        printf("%d %d\n", re1[i], re2[i]);}
  
 


Related Article

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.