HDU 4044 geodefense (tree-shaped DP, tree backpack, mixed Classic)

Source: Internet
Author: User

Test instructions

to a tree of n nodes, point 1 for the enemy base, the leaf node is our position. We can place bombs at each node, at most one per point, with Ki bombs on each node, and each bomb has a cost and a damage (1 damage to 1 hp for the enemy). The enemy's purpose is our position, any route is possible, but the rules can only go down. Q When there is M money, the worst case is how many HP can be knocked out of the enemy? (n<1001, m<201, ki>=0)

Ideas:

I actually wrote it myself, a click ~ to prove that the problem is not difficult!

Note: Some points may not have the option of bombs (i.e. no bombs); There are 0 spent bombs; Worst case scenario is the path where the enemy always runs the smallest attack.

The subject can not say what the specific model, mixed. Observe that if this node does not put bombs, then all the sub-trees will have to put bombs, or the node down the attack must be 0. It should be more cost-effective to put the bomb closer to the root than possible.

Similarly, analyze a node. This node is the root of the subtree of attack =min (damage per subtree) + (the attack of this node), which is the short board principle. Then consider this node, if bought, directly on the short board to add damage, provided that this node only allowed to put 1 bombs.

How to transfer? Start with a backpack that costs 0 of the subtree, and if there's a better substitute for him. This step temporarily represents dp[t][j] as a subtree with T as the root, if you get J, you can at least attack the enemy's HP. State equation: Dp[t][j]=max (dp[t][j], min (Dp[t][j-k], dp[e.to][k]));. Why is there a min? Because the short board is to be taken. Be aware of the initialization problem.

Because there may be free bombs, and each point only need to consider 1 free bombs (only 1), other free bombs removed (because it is not cost-effective AH), and free bombs always have to be considered first, otherwise it will be repeated.

  

1#include <bits/stdc++.h>2#include <cstdio>3#include <cstring>4#include <map>5#include <vector>6#include <iostream>7 #definePII pair<int,int>8 #defineINF 0x3f3f3f3f9 #defineLL Long LongTen using namespacestd; One Const intn=1010; A structTower - { -     intPri,pow; the}tow[n][ -]; -  - structnode - { +     int  from, To,next; - node () {}; +Nodeint  from,intTo,intNext): from( from), to, next (next) {}; A}edge[n*2]; at  - intEDGE_CNT, Head[n], cnt[n], dp[n][ About], N, m; - voidAdd_node (int  from,intto ) - { -Edge[edge_cnt]=node ( from, To, head[ from]); -head[ from]=edge_cnt++; in } -InlineintCMP (Tower A,tower b) {returna.pri<B.pri;} to  + voidDFS (intTintFar ) - { the node E; *     intflag=0; $      for(intI=HEAD[T]; i!=-1; I=e.next)Panax Notoginseng     { -E=Edge[i]; the         if(E.to==far)Continue; + DFS (e.to, t); A  the          for(intJ=m; j>=0; j--)//capacity +         { -Dp[t][j]=min (Dp[t][j], dp[e.to][0]);//The cost of the first load is 0, if there is a better replacement.  $              for(intk=1; k<=j; k++)//give this child K-yuan, get the maximum attack damage.  $Dp[t][j]=max (Dp[t][j], min (dp[t][j-K], dp[e.to][k])); -         } -flag=1;//mark whether the leaves.  the     } -     if(flag==0) memset (Dp[t],0,sizeof(Dp[t]));//leaf nodeWuyi  the     //The decision of this node is 01 backpack model: Buy or not buy. (You can only rely on children to protect yourself) -      for(intJ=m; j>=0; j--)//capacity Wu     { -         intk=1; About          for(; k<cnt[t] &&!tow[t][k].pri; k++)//no money, only 1 of the biggest power. $             if(tow[t][k-1].pow >Tow[t][k].pow) -Swap (tow[t][k],tow[t][k-1]); -  -          for(k--; k<cnt[t]; k++)//Items A         { +             if(j-tow[t][k].pri>=0) theDp[t][j]=max (Dp[t][j], Dp[t][j-tow[t][k].pri] + Tow[t][k].pow);//can be directly blocked -         } $     } the } the  the intMain () the { -Freopen ("Input.txt","R", stdin); in     intT, a, B; Cin>>T; the      while(t--) the     { AboutEdge_cnt=0; theMemset (Head,-1,sizeof(head)); theMemset (DP,0x3f,sizeof(DP));//Note Initialization the  +scanf"%d",&n); -          for(intI=1; i<n; i++)//graph without Direction the         {Bayiscanf"%d%d",&a,&b); the Add_node (A, b); the Add_node (b,a); -         } -  thescanf"%d", &m);//money.  the          for(intI=1; i<=n; i++) the         { thescanf"%d",&cnt[i]); -              for(intj=0; j<cnt[i]; J + +) thescanf"%d%d", &tow[i][j].pri, &Tow[i][j].pow); theSort (Tow[i], tow[i]+cnt[i], CMP);//free row in front the         }94DFS (1, -1); theprintf"%d\n", dp[1][m]); the     } the     return 0;98}
AC Code

HDU 4044 geodefense (tree-shaped DP, tree backpack, mixed Classic)

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.