Poj-1861,network, minimum generation tree water problem, pay attention to problems on the surface output, do not bother ~ ~

Source: Internet
Author: User
Tags integer numbers

Network
Time Limit: 1000MS Memory Limit: 30000K
Special Judge

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

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

I admit to being bored, on Baidu to find the minimum spanning tree (link), and then the first is the question, hey, although just learned, but for this is not very complex spanning tree problem, I still have confidence,, just look at the sample to see a while, if it is the smallest spanning tree, then the surface output ... Looked at the discussion area to confirm my conjecture;

AC Code:

#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < algorithm>using namespace Std;const int n=20000+10;int n,m,f[1001];struct node{int u,v,w,vis;} a[n];int CMP (Node A , Node B) {return A.W&LT;B.W;} int find (int x) {return f[x]==-1?x:<span style= "color: #ff0000;"    >x=find (F[x]) </span>;//Here the very difficulty of error,}void ks (int n) {memset (f,-1,sizeof (f));    Sort (a,a+m,cmp);    int ans=-1,cot=0;        for (int i=0; i<m; i++) {int u=find (A[I].U);        int V=find (A[I].V);            if (u!=v) {f[u]=v;            a[i].vis=1;//mark; cot++;    Ans=max (ANS,A[I].W);//find the longest side;}} printf ("%d\n%d\n", Ans,cot); for (int i=0; i<m; i++) if (A[i].vis) printf ("%d%d\n", A[I].U,A[I].V);}        int main () {while (~scanf ("%d%d", &n,&m)) {memset (a,0,sizeof (a));        for (int i=0; i<m; i++) scanf ("%d%d%d", &AMP;A[I].U,&AMP;A[I].V,&AMP;A[I].W); Ks(n); } return 0;}

Poj-1861,network, minimum generation tree water problem, pay attention to problems on the surface output, do not bother ~ ~

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.