Hdu 4003 Find Metal Mineral 树型背包DP

来源:互联网 发布:建筑图纸设计软件 编辑:程序博客网 时间:2024/06/03 16:08

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.
 

Output
For each cases output one line with the minimal energy cost.
 

Sample Input
3 1 11 2 11 3 13 1 21 2 11 3 1
 

Sample Output
32
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


一棵树,根为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;}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 体脂称身体年龄比实际年龄大怎么办 分手两天我好难过好想去找他怎么办 宝宝一吃药就哭得很厉害怎么办 比你入职晚的同事比你厉害怎么办 nba篮球大师能力值满了怎么办 做什么都觉得很累没有兴趣怎么办 剧烈运动后头晕恶心想吐怎么办 剧烈运动完头晕恶心想吐怎么办 小孩头部摔到地上想吐头晕怎么办 肺部化疗恶心反胃怎么办吃什么药好 好久没运动一运动全身酸痛怎么办 怀孕两个月了一直吐没体力怎么办 公务员审查六级口语证丢了怎么办 农资经营如果被执法查处了怎么办? 干隧道测量坚持不下去该怎么办 老家房子强拆没钱做没钱补怎么办? 魔兽世界出了两个一样的传说怎么办 死亡事故后遇上不讲理家属怎么办 死亡事故后死亡者家属不讲理怎么办 住的地方治安乱混混多怎么办 使命召唤进去感觉动作好慢怎么办 40岁后不够硬不持久怎么办 如果别人毁灭了你的梦想怎么办 如果死者托梦说自己过的不好怎么办 吵架了老婆跑回娘家不回家了怎么办 和老婆吵架她跑回娘家了怎么办 鞋子里面的皮穿的都是泥怎么办 绝地求生手游模拟器键位不灵怎么办 我更新目录的时候目录不存在怎么办 如果别人欠我钱不还 我该怎么办 老婆欠信用卡的钱我该怎么办 我欠了很多钱我该怎么办 一个人欠我钱跑了我该怎么办 买房子时间长了成危楼了怎么办 别人欠我钱人找不到了怎么办 欠银行信用卡钱人失踪了怎么办 欧洲卡车模拟2没油了怎么办 蓝牙安装包里没有微信怎么办 腾讯手游模拟器玩全军出击卡怎么办 手游cf用模拟器画面很抖怎么办 腾讯手游助手模拟器游戏卡死怎么办