Poj 1861 Network

Source: Internet
Author: User

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

Very similar to the previous topic, more interesting is a bit deceptive here, but I did not notice, Fluke. Fortunately, the sample did not look closely. The topic is only required to find the longest edge in the case of ensuring that all vertices are linked. The total weight of the path is not required to be minimal. So you can add any additional edges that are less than the longest edge weight, although he may be a ring. All have a wonderful sample. In fact, the idea of constructing the minimum spanning tree is completely.

#include <iostream> #include <algorithm> #include <string.h> #include <stack> #include < queue>using namespace Std;const int m=10046;int f[m];int r[m];int n,m;int res,num;struct edge{int u,v,w;} per[15005 ];bool CMP (Edge A, Edge b) {return A.W&LT;B.W;}        void Init () {for (int i=1; i<=n; i++) {r[i]=1;    F[i]=i;    }}//int find (int x)//{//if (x!=f[x])//Return F[x]=find (F[x]);//}int find (int x) {int r=x;    while (R!=f[r]) {r=f[r];    } int k=x;        while (k!=r) {int t=f[k];        F[k]=r;    k=t; } return R;}    void Union_set (int x,int y) {int tx=find (x);    int Ty=find (y);    if (tx==ty) return;    else if (R[tx]>r[ty]) f[ty]=tx;    else if (R[tx]<r[ty]) f[tx]=ty;        else {f[tx]=ty;    r[ty]++;    }}void Kru () {//stack<int>cnt;    queue<int>cnt;    Res=-1;   for (int i=0; i<m; i++) {if (Find (per[i].u)!=find (PER[I].V)) {         num++;            if (RES&LT;PER[I].W) RES=PER[I].W;            Union_set (PER[I].U,PER[I].V);        Cnt.push (i);            } if (num==n-1) {cout<<res<<endl<<cnt.size () <<endl;                while (!cnt.empty ()) {int T=cnt.front ();                Cnt.pop ();            cout<<per[t].u<< "" <<per[t].v<<endl;        } break;        }}}int Main () {while (cin>>n>>m) {num=0;        Init ();        for (int i=0; i<m; i++) {cin>>per[i].u>>per[i].v>>per[i].w;        } sort (per,per+m,cmp);    Kru (); } return 0;}


Poj 1861 Network

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.