Rivers ioi2005 Tree DP

Source: Internet
Author: User

To tell the truth, after writing this question, very want to vomit a mouthful of blood out, to show my mood is bad;

The topic is very simple, the tree-shaped DP, normally do 30 minutes, obstinately did for several hours, really sad.

The puzzle is not written, just spit a groove, online no backpack write DP, all is left son right brother writing, can not control a bit, uncomfortable ah.

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineLL Long Long6 intn,k;7 Const intmaxn= About;8 ConstLL inf=3000000000LL;9 structnode{Ten     inty,next,v; One }E[MAXN]; A intlinkk[maxn],len=0, w[maxn],d[maxn][maxn],siz[maxn],q[maxn],tail=0, FA[MAXN],RU[MAXN],VIS[MAXN]; - intRead () { -     intx=0;CharCh=getchar ();BOOLflag=0; the      while(ch<'0'|| Ch>'9'){if(ch=='-') flag=1; ch=GetChar ();} -      while(ch<='9'&&ch>='0') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnflag?-x:x; - } + voidInsertintXintYintv) { -e[++len].y=y; +e[len].v=v; Ae[len].next=Linkk[x]; atlinkk[x]=Len; - } - voidPrintintx) {printf ("%d\n", x);} - voidPrintintXintY) {printf ("%d%d\n", x, y);} - voidinit () { -     inty,v; inN=read (), k=read (); -memset (D,Ten,sizeof(d)); to      for(intI=1; i<=n;i++){ +W[i]=read (), Y=read (), v=read (); - Insert (i,y,v); insert (y,i,v); thed[i][y]=v,d[y][i]=v; *     } $     Panax Notoginseng } - voidfindd () { the      for(intI=1; i<=n;i++) d[i][i]=0; +      for(intk=0; k<=n;k++) A          for(intI=0; i<=n;i++) the              for(intj=0; j<=n;j++) +                 if(D[i][k]+d[k][j]<d[i][j]) d[i][j]=d[i][k]+D[k][j]; - } $LL f[ the][ -][ the]; $ voidDfsintXintf) { -siz[x]=1; -fa[x]=F; the      for(intI=linkk[x];i;i=E[i].next) { -         if(e[i].y==f)Continue;Wuyi DFS (e[i].y,x); thesiz[x]+=SIZ[E[I].Y]; -ru[x]++; Wu     } -     if(siz[x]==1) q[++tail]=x; About } $ voidWork () { -memset (f,-1,sizeof(f)); - findd (); -Dfs0,0); A      for(intHead=1; head<=tail;head++){ +         intx=Q[head]; theru[fa[x]]--; -         if(ru[fa[x]]==0) q[++tail]=Fa[x]; $LL g[ About]; the          for(intk=0; k<=siz[x]&&k<=k;k++){ the              for(intPREV=FA[X];p rev!=-1;p rev=Fa[prev]) { the                  the                  for(intp=0;p <=k;p++) g[p]=0; -                  for(intI=linkk[x];i;i=E[i].next) { in                     if(E[i].y==fa[x])Continue; theLL minn=inf; the                      for(intj=k;j>=0; j--){ Aboutminn=inf; the                          for(intL=0; l<=siz[e[i].y]&&l<=j;l++) theMinn=min (minn,g[j-l]+F[e[i].y][l][prev]); theg[j]=Minn; +                     } -                 } theLL ans=g[k]+w[x]*D[prev][x];Bayi                  for(intp=0;p <=k;p++) g[p]=0; the                  for(intI=linkk[x];i;i=E[i].next) { the                     if(E[i].y==fa[x])Continue; -LL minn=inf; -                      for(intj=k-1; j>=0; j--){ theminn=inf; the                          for(intL=0; l<=siz[e[i].y]&&l<=j;l++) theMinn=min (minn,g[j-l]+f[e[i].y][l][x]); theg[j]=Minn; -                     } the                 } theF[x][k][prev]=min (ans,k-1<0? inf:g[k-1]); the                 if(!prev) Break;94             } the         } the     } thecout<<f[0][k][0]<<Endl;98 } About intMain () { -Freopen ("1.in","R", stdin);101Freopen ("1.out","W", stdout);102 init ();103 Work ();104}
View Code

Rivers ioi2005 Tree DP

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.