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