题目传送门
Problem Description
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
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
Sample Output
3
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;
Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
题意
有K个机器人,走完树上的全部路径,每条路径有个消费。对于一个点,机器人可以出去再回来,开销2倍。也可以不回来,一直停在某个点(如果你的机器人数量足够多的话)。问最小开销。
题解
-
其实这题只能说是类树形背包。
-
用dp[i][j]表示在i点,有j个不回来的机器人走过的最小开销。
比如dp[i][0]就表示,i点及其子点全部靠其它点的不回来的机器人探索。所以机器人是一来一回开销2倍。
for(K…j…0) //j可以为0
dp[i][j]+=dp[t][0]+2*e[a].w; //表示子点t全靠其它点的机器人探索
for(1…k…j) //k要从1开始了,且k最大可以为j,也就是说父亲点可以是0个机器人,子点的机器人下去之后又回到子点,又去探索新的子点的子点。这样必然有父亲点0的情况。
-
状态转移方程:dp[i][j]=min(dp[i][j],dp[i][j-k]+dp[t][k]+k*e.w);
AC-Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);const int INF = 0x7fffffff;
const int MAXN = 1e4 + 5;
const int MOD = 1e9 + 7;struct Edge {
int next;int to;int w;
}edge[MAXN << 1];
int head[MAXN];
int cnt;
int dp[MAXN][15];
int son[MAXN];
int n, root, k;
void addEdge(int u, int v, int w) {
edge[cnt].to = v;edge[cnt].w = w;edge[cnt].next = head[u];head[u] = cnt++;
}
void init() {
memset(head, -1, sizeof head);memset(dp, 0, sizeof dp);memset(son, 0, sizeof son);cnt = 0;
}
void dfs(int u, int fa) {
son[u] = 1;for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;if (v == fa) continue;dfs(v, u);son[u] += son[v];for (int j = ::k; j >= 0; j--) {
dp[u][j] += dp[v][0] + 2 * edge[i].w;for (int k = 1; j - k >= 0; k++) {
dp[u][j] = min(dp[u][j], dp[u][j - k] + dp[v][k] + k * edge[i].w);}}}
}
int main() {
ios;while (cin >> n >> root >> k) {
init();for (int i = 1; i < n; i++) {
int a, b, c;cin >> a >> b >> c;addEdge(a, b, c);addEdge(b, a, c);}dfs(root, -1);cout << dp[root][k] << endl;}return 0;
}