CSU 1541 there is No alternative (minimum spanning Tree + enumeration)

Source: Internet
Author: User

Topic Link: Portal

Test instructions

There are n points, M edges, to make all n points together and to spend the least, ask which side is necessary to connect.

Analysis:

To minimize the cost of making sure is to make the smallest spanning tree, but the topic requires which sides are necessary to use. We can

To think like this, we first ask for the smallest spanning tree and then count the edges

Lift This n-1 the edges so they can't be used, and then continue to do the minimum spanning tree if the last request value and first

If not, then this side is sure to be connected.

The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring>using namespace    std;const int MAXN = 50010;struct nod{int x,y,val;    BOOL operator < (const struct NOD &tmp) const{return this->val<tmp.val; }}edge[maxn];int par[maxn/100],num[maxn/100];int id[maxn/100];int n,m;void init () {for (int i=0;i<=n;i++) par[i]=i, Num[i]=1;}    int find_par (int x) {if (x!=par[x]) return Par[x]=find_par (Par[x]); return par[x];}    BOOL Union (int x,int y) {x=find_par (x);    Y=find_par (y);        if (x!=y) {par[y]=x;        Num[x]+=num[y];    return true; } return false;}    int ans, cnt,sum1,sum2;void solve () {sum1=sum2=0;        for (int i=0;i<cnt;i++) {int tmp=0;init (); for (int j=0;j<m;j++) {if (J!=id[i]) {if (Union (EDGE[J].X,EDGE[J].Y)) tmp+=ed            Ge[j].val;    }} if (Tmp!=ans) Sum1++,sum2+=edge[id[i]].val; } printf ("%d%d\n", sum1,sum2);} INT Main () {while (~scanf ("%d%d", &n,&m)) {init ();        for (int i=0;i<m;i++) {scanf ("%d%d%d", &edge[i].x,&edge[i].y,&edge[i].val);        } sort (edge,edge+m);        ans=0,cnt=0;        Memset (id,0,sizeof (id));                for (int i=0;i<m;i++) {if (Union (EDGE[I].X,EDGE[I].Y)) {ans+=edge[i].val;            Id[cnt++]=i;    }} solve (); } return 0;} /***4 41 2 31 3 52 3 32 4 34 41 2 31 3 12 3 32 4 33 31 2 12 3 11 3 1***/


CSU 1541 there is No alternative (minimum spanning Tree + enumeration)

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.