Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3696 Accepted Submission(s): 1726
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.
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 2HintIn the first case: 1->2->1->3 the cost is 3; In the second case: 1->2; 1->3 the cost is 2;
Source
一棵树,根为s,从s点投放k个机器人,要求这k个机器人将树上所有点遍历,每走一次树上的边,代价就是边权。机器人完成任务后可以从树上任何节点消失。求最少代价。
第一次碰到这种题----树型背包。
看了下题解,大概明白了。
dp[i][j]表示对于i号节点的子树,消耗j个机器人的最小代价。(即j个机器人在i号节点的子树中消失)
对于每个儿子节点son,dp[i][j]=min(dp[i][j-k]+dp[son][k]+k*edge.cost),表示k个机器人从i节点下去,不再上来的代价。
那么最极端的情况是,所有机器人下去之后再回到i节点。这时的代价是2*edge.cost+dp[to][0].
用类似背包的方式实现DP。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=10005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int dp[maxn][11];
int head[maxn];
bool visit[maxn];
int num,p;
struct Edge {
int from,to,pre,dist;
};
Edge edge[maxn*2];
void addedge(int from,int to,int dist) {
edge[num]=(Edge){from,to,head[from],dist};
head[from]=num++;
edge[num]=(Edge){to,from,head[to],dist};
head[to]=num++;
}
void dfs(int now) {
visit[now]=1;
int i,j,k;
for (i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (!visit[to]) {
dfs(to);
for (k=p;k>=0;k--) {
dp[now][k]+=dp[to][0]+2*edge[i].dist;
for (j=1;j<=k;j++) {
dp[now][k]=min(dp[now][k],
dp[now][k-j]+dp[to][j]+j*edge[i].dist);
}
// cout << dp[now][k] << endl;
}
}
}
}
int main() {
int n,s;
while (scanf("%d%d%d",&n,&s,&p)!=EOF) {
int i,x,y,d;
num=0;
memset(head,-1,sizeof(head));
mem0(visit);mem0(dp);
for (i=1;i<n;i++) {
scanf("%d%d%d",&x,&y,&d);
addedge(x,y,d);
}
dfs(s);
printf("%d\n",dp[s][p]);
}
return 0;
}