[HDU - 4003] Find Metal Mineral
树形dp,分组背包
Find Metal Mineral
Humans 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 should 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 should make a optimal plan which cost minimal energy cost.
Input
There 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 should cost w.
1<=N<=10000, 1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000.
Output
For each cases output one line with the minimal energy cost.
Sample Input
1 | 3 1 1 |
Sample Output
1 | 3 |
Notes
In the first case: 1->2->1->3 the cost is 3;
In the second case: 1->2; 1->3 the cost is 2;
题意
给你k(k<=10)个机器人,在一颗树的根节点上开始遍历,经过一条边就会有一定的代价(即为边权),问遍历到所有的点的最小代价是多少
题解
考虑设为以u为根节点的树消耗k给机器人(这k给机器人不回来),遍历完整棵子树的最小代价。其中特数情况即为遍历完整颗子树不消耗机器人(机器人都回到u点,便可以继续使用,故不消耗)。
这是一个分组背包。考虑转移:
要特殊处理:dp[u][k]+=dp[v][0]+2*wth[i]
乘2是因为一个机器人下去又回来,u->v走两次
一般情况:dp[u][k]=min(dp[u][k],dp[u][k-j]+dp[v][j]+j*wth[i])
其中乘是因为这j个机器人下去不回来,经过u->v这条边次
code
1 |
|