Algorithm: HDU 1011 Starship Troopers (tree-type backpack DP)

Source: Internet
Author: User
Tags continue

Meaning

There are n caves numbered 1~n, there are passages between the caves, forming a n-1 tree, the entrance of the cave is the root node is 1.

Each cave has x only bugs, and has the value Y gold, all eliminates a cave the worm, can obtain this cave's Y gold.

Now send M a warrior to find the gold and enter from the entrance. Each time only after the destruction of the current cave of all the worms, you can choose to enter the next cave.

A warrior can destroy 20 bugs, if you want to kill X worm, then to x/20 up the whole that is (x+19)/20 soldiers.

If you want to get the gold of a cave, you must leave enough warriors to kill all the bugs, that is, (x+19)/20 soldiers, and then these soldiers will not be able to go to other caves.

Other fighters can continue to walk to other caves and choose to group into different caves.

A warrior can only walk deep into a cave, not backtrack.

Ask how much gold can be obtained at most?

Ideas

This problem at first did not understand good question, so WA a number of times, understand good question is not difficult.

We can know the cost per node (i) = (x (i) +19)/20.

So

F (i, J): Represents I subtree, with the value of J-Warrior maximum can be obtained.

If I have a son of S node, then formed the s group of items, each group of items for packet backpack.

Each group can choose to send 1,2...j-cost (i) A soldier to go, why most is j-cost (i)? Because still have to leave the cost (i) A warrior kills the worm in the current cave.

This will allow for a state transition:

F (i, j) = max {max{f (i, j-k) + f (V, K) | 1<=k<=j-cost (i)} | V is the son of I node}

It is important to note that if it is a leaf node and the cost of the leaf node is 0, then it will cost him 1, because a warrior must be sent to the leaf node to get the gold.

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: hdu- 1011 Starship Troopers * @description: Tree-Type Backpack DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshu
angde@gmail.com * Copyright (C) 2013/08/18 20:27 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
    
Const double PI = ACOs (-1.0);
    
const int MAXN = 110;
int n, m; struct Node {int num,cost, Val;}
ROOM[MAXN];
int F[MAXN][MAXN];
    
BOOL VIS[MAXN];
    
vector<int>adj[maxn];
    
void Dfs (int u) {Vis[u] = true; memset (F[u], 0, sizeof (f[u)); If it is a leaf node and spends 0 o'clock, it will become 1//http://www.bianceng.cn if (!roOm[u].cost && adj[u].size () ==1 && u!=1) room[u].cost = 1;
    
Initialize for (int i = Room[u].cost i <= m; ++i) f[u][i] = Room[u].val; for (int i = 0; i < adj[u].size (); ++i) {int v = adj[u][i]; if (vis[v)) continue; Dfs (v), for (int s = M >= room [U].cost;
--s) {for (int j = 1; s-j >= room[u].cost; ++j) F[u][s] = max (F[u][s], f[u][s-j] + f[v][j);} int main () {while (~SCANF ("%d%d", &n, &m) && n+m!=-2) {for (int i = 1; I <= N; ++i
    
) Adj[i].clear ();
    
for (int i = 1; I <= n; ++i) {int x; scanf ("%d%d", &x, &room[i].val); room[i].cost = X/20 + (x%20!=0);}
for (int i = 0; i < n-1 ++i) {int u, v scanf ("%d%d", &u, &v); Adj[u].push_back (v); Adj[v].push_back (U);
    
} if (m==0) {puts ("0"); continue;}
memset (Vis, 0, sizeof (VIS));
Vis[1] = true;
DFS (1);
printf ("%d\n", F[1][m]);
return 0; }

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.