Topic background
mzc and djn is ... not well known, so we're going to promote it.
Title Description
mzc family is rich (joke), his family has n male servant, now mzc want to gather them all together (do not know). Now know mzc and male servant who communicate with each other between time, please calculate the total time to each of them called need (to be repeated oh). They can ensure that everyone called.
Input Format
The first row has a number n, there are n M represents retainers. The second row has a number of m and m represents a communication route. After the m rows, each row of three numbers a [i], b [i ], c [i], represents a [i] male retainers (or MZC) and b [i] male retainers (or MZC) communication takes time (two-way). A I = 0 indicates mzc.
Output Format
Line, a number sum, to represent every one of them called the total time required.
Sample input and output
5 12 0 2 15 2 3 20 3 5 13 1 3 29 0 1 30 2 4 21 0 3 23 5 1 48 0 4 17 0 5 27 1 2 43 2 5 41
94
Description / Tips
n<=2300
m<=400000
Solution: Forgotten minimum spanning tree.
#include<iostream> #include<algorithm> #include<queue> #include<cmath> #include<cstring> #include<cstdlib> #include<cstdio> using namespace std; const int N=400005; int n,ans,m,biu; struct node{ int uu,vv,w; }e[N]; int dis[N],head[N],cnt,fa[N]; bool vis[N]; bool cmp(node a,node b) { return a.w<b.w; } int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; } void init(){ scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ scanf("%d %d %d",&e[i].uu,&e[i].vv,&e[i].w); e[i].uu++; e[i].vv++; } } void Yao_Chen_Lai_Le(){ int u,v; sort(e+1,e+m+1,cmp); for(int i=1;i<=m;i++){ u=find(e[i].uu); v=find(e[i].vv); if(u==v) continue; ans+=e[i].w; fa[u]=v; biu++; //if(biu==(n-1)) break; } } int main(){ freopen("2126.in","r",stdin); freopen("2126.out","w",stdout); init(); Yao_Chen_Lai_Le(); printf("%d",ans); return 0; }