P3366 [TEMPLATE] Minimum Spanning Tree and p3366 template Spanning Tree

Source: Internet
Author: User

P3366 [TEMPLATE] Minimum Spanning Tree and p3366 template Spanning Tree
Description

For example, an undirected graph is given to find the Minimum Spanning Tree. If the graph is not connected, the output is orz.

Input/Output Format Input Format:

The first line contains two integers N and M, indicating that the graph has N nodes and M undirected edges. (N <= 5000, M <= 200000)

Each row in the next M line contains three integers Xi, Yi, and Zi, indicating that there is an undirected EDGE connection node Xi and Yi with the length of Zi.

Output Format:

The output contains a number, that is, the sum of the length of each edge of the Minimum Spanning Tree. If the graph is not connected, the output is orz.

Input and Output sample Input example #1:
4 51 2 21 3 21 4 32 3 43 4 3
Output sample #1:
7
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 20% of data: N <= 5, M <= 20

For 40% of data: N <= 50, M <= 2500

For 70% of data: N <= 500, M <= 10000

For 100% of data: N <= 5000, M <= 200000

Example:

Therefore, the total Edge Weight of the Minimum Spanning Tree is 2 + 2 + 3 = 7.

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=200001; 7 struct node 8 { 9     int u,v,w;10 }edge[MAXN];11 int f[MAXN];12 int comp(const node & a,const node & b)13 {14     return a.w<b.w;15 }16 int find(int x)17 {18     if(f[x]!=x)19     f[x]=find(f[x]);20     return f[x];21 }22 void unionn(int x,int y)23 {24     int fx=find(x);25     int fy=find(y);26     f[fx]=fy;27 }28 int main()29 {30     int n,m;31     scanf("%d%d",&n,&m);32     for(int i=1;i<=n;i++)33     f[i]=i;34     for(int i=1;i<=m;i++)35     {36         scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);37     }38     sort(edge+1,edge+m+1,comp);39     int k=0;40     int ans=0;41     for(int i=1;i<=m;i++)42     {43         if(find(edge[i].u)!=find(edge[i].v))44         {45             unionn(edge[i].u,edge[i].v);46             ans+=edge[i].w;47             k++;48         }49         if(k==n-1)break;50     }51     if(k!=n-1)52     printf("orz");53     else printf("%d",ans);54     return 0;55 } 

 

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.