HDU 4003 Find Metal Mineral (tree DP + grouping backpack)

Source: Internet
Author: User
Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission (s): 1441 Accepted Submission (s): 631

Problem DescriptionHumans have discovered a kind of new metal mineral on Mars which are distributed in point-like with paths connecting each of them which formed a tree. now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot shocould start their job at point S. each robot can return to Earth anywhere, and of course they cannot go back to Mars. we have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. to reduce the total energy cost, we shocould make a optimal plan which cost minimal energy cost.

 

InputThere are multiple cases in the input.
In each case:
The first line specifies three integers N, S, K specifying the numbers of metal mineral, landing site and the number of robots.
The next n-1 lines will give three integers x, y, w in each line specifying there is a path connected point x and y which shoshould cost w.
1 <= N <= 10000, 1 <= S <= N, 1 <= k <= 10, 1 <= x, y <= N, 1 <= w <= 10000.

 

OutputFor each cases output one line with the minimal energy cost.

 

Sample Input3 1 1 1 1 1 1 3 1 1 1 2 2 1 1 3 1

 

Sample Output3 2

Hint

In the first case: 1-> 2-> 1-> 3 the cost is 3; In the second case: 1-> 2; 1-> 3 the cost is 2;

 

 

SourceThe 36th ACM/ICPC Asia Regional Dalian Site -- Online Contest

 

Recommendlcy

-- Minimum cost required for K robots to traverse all vertices from the same point

-- Tree DP, group backpack

-- Url: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4003

------------------------------------------------

[Reprinted ]:

Dp [I] [j] indicates the weights and values required by j robots for the subtree with the I node as the root node.

When j = 0, it indicates that a robot is put down, and the traversal end point is returned to the I node. The state transfer equation is similar to a backpack.

If j (j> 0) robots exist in the tree with I as the root node in the final state, it is impossible for other robots to r to this tree and then run to other trees.

In that case, it will definitely return to I after one of j reaches I and runs the same path as r, then the path for running it should be different (an I-return edge is added)

In this case, if no robot exists in the tree with I as the root node, it is possible to send a robot to traverse the tree and then return.

------------------------------------------------

Status transfer, using the "Group backpack" idea.

The pseudocode for using a one-dimensional array as follows:

For all Group I

For v = V .. 0

For all k belong to Group I

F [v] = max {f [v], f [v-c [k] + w [k]}

------

It can be understood as follows:

Each root node has a backpack with a capacity of K.

If it has an I-son, it has an I-group item. The values are dp [son] [0], dp [son] [1] ...... dp [son] [k]. The weights of these items are 0, 1 ,..... k

Now, you need to select an item from each group (and you must select an item) and load it into the root backpack, which makes the maximum value when the capacity does not exceed k.

This is a problem with grouping backpacks.

But there is a problem here, that is, each group must select an item.

For this processing, we first put dp [son] [0] into the backpack. If there is a better choice in this group, the item will be replaced, otherwise, this item is the best choice. In this way, each group must be selected.

 

 

 

/* HDU 4003 tree-like DP + can only select one item grouping backpack dp [pos] [num] indicates the sub-tree with pos as the root node, use num robot, the minimum value obtained is especially when num = 0, dp [pos] [0] indicates that a robot is used to complete all sub-trees, finally, return to the pos node status transition: dp [pos] [num] = min Σ {dp [pos_j] [num_j] + w_j}. pos_j is the son of all pos, to select only one group backpack, first let dp [u] [0]. For example, if the backpack uses a one-dimensional array, the "Group backpack" pseudocode is as follows: for all groups I for v = V .. 0 for all k belong to group I f [v] = max {f [v], f [v-c [k] + w [k]} */# include <stdio. h> # include <algorithm> # include <string. h> # include <iostream> using namespace std; const int MAXN = 10010; struct Node {int to; int next; int cost; // cost of the path} edge [MAXN * 2]; // undirected tree int head [MAXN]; int tol; int dp [MAXN] [11]; // dp [I] [j] indicates that j people are used in the tree where I is the root. Dp [I] [0] indicates that a person returns to the preceding void init () {memset (head,-1, sizeof (head); tol = 0; memset (dp, 0, sizeof (dp);} void add (int a, int B, int val) {edge [tol]. to = B; edge [tol]. cost = val; edge [tol]. next = head [a]; head [a] = tol ++; edge [tol]. to = a; edge [tol]. cost = val; edge [tol]. next = head [B]; head [B] = tol ++;} int N, K; void dfs (int u, int pre) {for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; if (v = pre) continue; dfs (v, u); for (int k = K; k> = 0; k --) {dp [u] [k] + = dp [v] [0] + 2 * edge [I]. cost; // put dp [u] [0] into the backpack first, and make sure to select at least one/grouping backpack for (int j = 1; j <= k; j ++) dp [u] [k] = min (dp [u] [k], dp [u] [k-j] + dp [v] [j] + j * edge [I]. cost) ;}}int main () {int S; int a, B, val; while (scanf ("% d", & N, & S, & K )! = EOF) {init (); for (int I = 1; I <N; I ++) {scanf ("% d", &, & B, & val); add (a, B, val);} dfs (S,-1); printf ("% d \ n ", dp [S] [K]);} 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.