POJ 1861 & ZOJ 1542 Network (Krusal of the smallest spanning tree)

Source: Internet
Author: User
Tags integer numbers

Topic Links:

pku:http://poj.org/problem?id=1861

zju:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=542


Description

Andrew is working as system administrator and was planning to establish a new network in he company. There'll is N hubs in the company, they can is connected to each of the other using cables. Since each worker of the company must has access to the whole network, each hub must is accessible by cables from any oth ER hub (with possibly some intermediate hubs).
Since cables of different types be available and shorter ones are cheaper, it's necessary to make such a plan of hub con Nection, that's the maximum length of a single cable is minimal. There is another problem-not each hub can being connected to any other one because of compatibility problems and building g Eometry limitations. Of course, Andrew would provide you all necessary information about possible hub connections.
You is to help Andrew to find the "the" to connect hubs so, all above conditions is satisfied.

Input

The first line of the input contains, numbers:n-the number of hubs in the network (2 <= N <=) and M-the number of possible hub connections (1 <= M <= 15000). All hubs is numbered from 1 to N. The following M lines contain information about possible connections-the numbers of both hubs, which can be connected and The cable length required to connect them. Length is a positive integer number this does not exceed 106. There'll is no more than one-to-connect, hubs. A Hub cannot is connected to itself. There always is at least one and connect all hubs.

Output

Output first the maximum length of a cable in your hub connection plan (the value of your should minimize). Then output your Plan:first output p-the number of cables used and then output P pairs of an integer numbers-numbers of hub s 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

Ps:

It seems that the case of the problem is a bit problematic, card for a long time!

The code is as follows:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int inf = 9999999; const int MAXN = 15017;int father[maxn];struct edge{int x,y,v;}; struct Edge Ed[maxn],ansa[maxn];bool cmp (Edge A,edge b) {return A.V&LT;B.V;}    int find (int x) {if (x==father[x]) return x; Return Father[x]=find (Father[x]);}    void krusal (int n,int m) {int i,fx,fy,cnt;    int ans=0;    for (i = 1; I <= n; i++) father[i]=i;    Sort (ed,ed+m,cmp);//The sort of side cnt=0;    int max=-1;        for (i=0; i<m; i++) {fx=find (ed[i].x);        Fy=find (ED[I].Y);            if (fx!=fy) {ans+=ed[i].v;            Father[fx]=fy;            ansa[cnt].x=ed[i].x;            ANSA[CNT++].Y=ED[I].Y;        if (MAX&LT;ED[I].V) max=ed[i].v;    }} printf ("%d\n%d\n", max,cnt); for (i=0; i<cnt; i++) printf ("%d%d\n", ansa[i].x,ansa[i].y);}    int main () {int t;    int n, m;    int A, b, K; while (scanf ("%d%d", &n,&m) {!=eof) {for (int i = 0; i < m; i++) {scanf ("%d%d%d", &a,&b,&k);        Ed[i].x=a,ed[i].y=b,ed[i].v=k;    } krusal (N,m); } return 0;}


POJ 1861 & ZOJ 1542 Network (Krusal of the smallest spanning tree)

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.