bzoj2753: [SCOI2012] ski with time capsule

Source: Internet
Author: User

bfs+ Minimum tree graph +kruskal algorithm.

The smallest tree diagram is an image of the smallest generation of graphs, this can not take Kruskal algorithm or prim algorithm directly, otherwise it will be wrong.

Is the case of w[u][v]!=w[v][u].

And this problem with the Zhu Liu algorithm is certainly not feasible.

But the direction of the problem is not the nature of the side, but the height of the point is determined. This allows us to layer the minimum spanning tree.

If you add a point with a height of H, simply use the Kruskal algorithm to select the shortest edge, and it will not affect the subsequent selection.

So we changed the Kruskal algorithm to the end point height for the first keyword descending and the edge length for the second keyword ascending sort ... (You know, or CMP, um.) )

So it can be spicy.

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =100000+Ten;Const intMAXM =2000000+Ten;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}structEdge {intu,v,d; }E[MAXM];intH[MAXN],F[MAXN];intG[maxn],v[maxm],next[maxm],eid;intres1,n,m;Long LongRes2;BOOLVIS[MAXN];intQ[maxm],l,r,u;voidAddedge (intAintb) {V[eid]=b; Next[eid]=g[a]; g[a]=eid++;}BOOLCMP (Edge A,edge b) {if(H[A.V]!=H[B.V])returnH[a.v]>H[B.V]; returna.d<B.D; }intFindintx) {returnf[x]==x?x:f[x]=find (F[x]);} intMain () {memset (g,-1,sizeof(g)); N=read (); m=read ();  for(intI=1; i<=n;i++) h[i]=read ();  for(intI=1, u,v;i<=m;i++) {u=read (); V=read (); E[i].d=read (); if(h[u]>=H[v]) Addedge (U,V); if(h[v]>=H[u])        {Addedge (v,u); swap (u,v);} E[I].U=u; e[i].v=v; } sort (E+1, e+m+1, CMP); Vis[q[r++]=1]=1, res1=1;  while(l<r) {u=q[l++];  for(intI=g[u];~i;i=next[i])if(!Vis[v[i]]) {Vis[q[r++]=v[i]]=1; Res1++; }    }         for(intI=1; i<=n;i++) f[i]=i;  for(intI=1, ru,rv;i<=m;i++)if(vis[e[i].u]&&VIS[E[I].V]) {ru=find (E[I].U); RV=find (E[I].V); if(ru!=RV) {Res2+=E[I].D; F[RV]=Ru; }} printf ("%d%lld\n", Res1,res2); return 0;}

bzoj2753: [SCOI2012] ski with time capsule

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.