HDU 4276 the Ghost Blows light (tree shape)

Source: Internet
Author: User

Test instructions: Give a tree of n nodes, start 1, end N, the connected two nodes have a distance, each node has a value, give a time t. Q What is the maximum value achieved from 1 to N in a given time t?

Idea: first from 1 to N, if the total time is not enough to go, directly exit, otherwise the time deducted, these edge right set to 0, and then do the tree-shaped DP

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 intn,m;7 inttot,go[200005],first[200005],next[200005],val[200005];8 intc[200005],vis[200005],pre[200005],edge[200005],op[200005];9 intf[505][505],v[200005];Ten intRead () { One     intt=0, f=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -     returnt*F; the } - voidInsertintXintYintz) { -tot++; -go[tot]=y; +next[tot]=First[x]; -first[x]=tot; +val[tot]=Z; A } at voidAddintXintYintz) { -Insert (x, y, z); op[tot]=tot+1; -Insert (y,x,z); op[tot]=tot-1; - } - voidBFs () { -      for(intI=1; i<=n;i++) vis[i]=pre[i]=edge[i]=0; in     intH=0, t=1; -c[1]=1; vis[1]=1; to      while(h<=t) { +h++; -          for(intI=first[c[h]];i;i=Next[i]) { the             intPur=Go[i]; *             if(Vis[pur])Continue; $pre[pur]=C[h];Panax Notoginsengedge[pur]=i; -c[++t]=pur; thevis[pur]=1; +         } A     } the } + voidprework () { -      for(inti=n;i!=1; i=Pre[i]) { $m-=Val[edge[i]]; $val[edge[i]]=val[op[edge[i]]]=0; -     } - } the voidDfsintXintFA) { -      for(intI=0; i<=m;i++) f[x][i]=0;Wuyi      for(intI=first[x];i;i=Next[i]) { the         intPur=Go[i]; -         if(PUR==FA)Continue; Wu DFS (pur,x); -         intdis=Val[i]; Aboutdis*=2; $          for(intk=m;k>=dis;k--) -           for(intj=0; j+dis<=k;j++) -F[x][k]=std::max (f[x][k],f[pur][j]+f[x][k-j-dis]); -     } A      for(intI=0; i<=m;i++) +f[x][i]+=V[x]; the } - intMain () { $      while(SCANF ("%d%d", &n,&m)! =EOF) { the      if(n==0&&m==0)return 0; thetot=0; the       for(intI=1; i<=n;i++) first[i]=0; the       for(intI=1; i<n;i++){ -          intx, y, z inX=read (); Y=read (); z=read (); the Add (x, y, z); the      } About       for(intI=1; i<=n;i++) v[i]=read (); the BFS (); the prework (); the      if(m<0){ +printf"Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); -         Continue; the      }BayiDfs1,0); theprintf"%d\n", f[1][m]); the     } -     return 0; -}

HDU 4276 the Ghost Blows light (tree shape)

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.