P1340 Animal Trail Management

Source: Internet
Author: User

Transmission DoorIdeas:

The topic requires that the Benquan of the minimum spanning tree be output every time the connection is connected. If you apply the minimum spanning tree template online, you will definitely time out and consider offline processing. Record the time of each inserted edge, and once all the edges have been inserted, the minimum spanning tree can be obtained (by choosing the edges before and after the insertion time).

Standard Process:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<string>#include<vector>#include<stack>#include<deque>#include<queue>#include<map>#include<Set>using namespacestd;#defineMax (a) (a) > (b)? ( A):(B))#defineMin (a) < (b) ( A):(B))#defineMAXN 6001typedefLong LongLL; LL N,w,cnt,ans; LL FA[MAXN];structhh{LL L,r,w,tim;} T[maxn];inline ll Read () {ll xs=0, kr=1;Charls; LS=GetChar ();  while(!isdigit (LS)) {        if(! (ls^ $)) KR=-1; LS=GetChar (); }     while(IsDigit (LS)) {xs= (xs<<1) + (xs<<3) + (ls^ -); LS=GetChar (); }    returnxs*KR;} InlineBOOLcmpConstHh&l,Consthh&R) {    returnl.w<R.W;} Inline ll find (ll u) {if(U!=fa[u]) fa[u]=find (Fa[u]); returnfa[u];} InlinevoidKruskal (LL num) { for(LL i=1; i<=w;i++) fa[i]=i;  for(LL i=1; i<=w;i++)    {        if(T[i].tim>num)Continue; LL R1=find (T[I].L), r2=find (T[I].R); if(r1!=R2) {FA[R1]=R2; Ans+=T[I].W; CNT++; }        if(cnt==n-1) {printf ("%lld\n", ans);return;} } printf ("-1\n");}intMain () {n=read (); w=read ();  for(LL i=1; i<=w;i++) {T[I].L=read (); T[i].r=read (); T[i].w=read (); t[i].tim=i; } sort (T+1, t+w+1, CMP);  for(LL i=1; i<=w;i++) {ans=0, cnt=0;    Kruskal (i); }return 0;}

P1340 Animal Trail Management

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.