COJ500 Yang's Path Planning (MST) (I'm serious)

Source: Internet
Author: User

When LCT is used to maintain the spanning tree, the dynamic Edge (S,T,W) creates a new node x with a weight of Benquan W.

1. If s is not connected with T, then connect s-x,x-t, Answer +w

2. If s is connected with T, find out the maximum weight w2 on the s-t path, if W<W2, delete W2 's connecting edge, connect s-x,x-t, answer +W-W2

It's too much water, isn't it!

#include <cstdio>#include<cctype>#include<queue>#include<cstring>#include<algorithm>#defineLC Ch[x][0]#defineRC Ch[x][1]using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=10010;intch[maxn][2],FA[MAXN],PRE[MAXN],FLIP[MAXN],MX[MAXN],V[MAXN];voidMaintain (intx) {Mx[x]=x; if(V[mx[lc]]>v[mx[x]]) mx[x]=MX[LC]; if(V[mx[rc]]>v[mx[x]]) mx[x]=MX[RC];}voidPushdown (intx) {    if(!flip[x])return; Swap (LC,RC); FLIP[LC]^=1; flip[rc]^=1; flip[x]=0;}voidRotateintx) {    inty=pre[x],z=pre[y],d=ch[y][0]==x; Ch[y][d^1]=CH[X][D];p re[ch[x][d]]=y; ch[z][ch[z][1]==y]=x;pre[x]=Z; CH[X][D]=y;pre[y]=X;maintain (y);}intQ[maxn],top;voidSplay (intx) {     for(intI=x;i;i=pre[i]) q[++top]=i; if(top!=1) fa[x]=Fa[q[top]];  while(top) Pushdown (q[top--]);  while(pre[x]) rotate (x); Maintain (x);}voidAccessintx) {     for(inty=0; x;x=Fa[x]) {splay (x);p re[ch[x][1]]=0; fa[ch[x][1]]=x; ch[x][1]=y;pre[y]=x; Maintain (Y=x); }}voidMakeroot (intx) {access (x); splay (x); flip[x]^=1;}voidLinkintXintY) {makeroot (x); fa[x]=y;}voidCutintXintY) {makeroot (x); Access (y); splay (y); ch[y][0]=pre[ch[y][0]]=0; maintain (x);}intFindintx) {access (x); splay (x); while(ch[x][0]) x=ch[x][0];returnx;}intQueryintXinty) {makeroot (x); Access (y); splay (y) ;returnmx[y];}intS[MAXN],T[MAXN];intMain () {intN=read (), m=n* (n1) >>1, tot=n,ret=0;  for(intI=1; i<=m;i++) {S[i]=read (), T[i]=read (), v[++tot]=read (); if(Find (S[i])!=find (T[i]) Link (s[i],tot), link (tot,t[i]), ret+=V[tot]; Else        {            intP=query (S[i],t[i]);if(v[p]>V[tot]) {ret+=v[tot]-V[p]; Cut (P,s[p-n]); Cut (p,t[p-n]);            Link (s[i],tot); link (tot,t[i]); }}} printf ("%d\n", ret); return 0;}
View Code

COJ500 Yang's Path Planning (MST) (I'm serious)

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.