(中等) 树形分组背包 HDU 4003 Find Metal Mineral

Find Metal Mineral

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


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
 


题意:给出一颗树,给你一个起点和机器人的数量,问从这个起点出发,让这些机器人遍历所有的节点并且消耗的总能量最少
思路:我们用dp[i][j] 表示从i节点派出j个机器人遍历所有的儿子所需的最少能量。dp[i][0] 表示不消耗机器人遍历所有节点,就是说派出一个机器人把所有节点遍历并且回到i节点消耗的能量。所以dp[i][0] = dp[i][0]+dp[son][0]+2*cost;
这里可以看成是一个分组背包,对应一个节点相当于就是一组物品,其中的物品不能选超过一个,但由于题目要求每个节点都要遍历,所以对于每个节点的儿子节点的物品都要选取一个。即我们每次在进行dp前都要累加上一次dp的结果

代码:
 #include<cstdio>
#include<algorithm>
#include<sstream>
#include<set>
#include<cstring>
#include<iostream>
#include<map>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 10000+10;
const int inf = 0xfffffff;
#define LL long long

int dp[maxn][11];
bool vis[maxn];
int ptr,N,S,K;
struct Node
{
int v;
int cost;
Node *next;
}*first[maxn],edge[maxn*2];

void add(int x,int y,int c)
{
edge[++ptr].cost = c;
edge[ptr].v = y;
edge[ptr].next = first[x];
first[x] = &edge[ptr];
}
inline int min(int x,int y)
{
return x < y ? x : y;
}

void tp(int x)
{
vis[x] = true;
Node *p = first[x];
while (p)
{
int v = p->v;
int cost = p->cost;
if (!vis[v])
{
tp(v);
for (int i = K ; i >= 1 ; --i)
{
dp[x][i] += dp[v][0]+2*cost;
for (int j = 1 ; j <= i ; ++j) 
dp[x][i] = min(dp[x][i],dp[x][i-j]+dp[v][j]+j*cost);
}
dp[x][0] += dp[v][0]+2*cost;
}
p = p->next;
}
}

int main()
{
while (scanf("%d%d%d",&N,&S,&K)==3)
{
memset(vis,0,sizeof(vis));
memset(first,0,sizeof(first));
memset(dp,0,sizeof(dp));
ptr = 0;
for (int i = 0 ; i < N-1 ; ++i)
{
int x , y , c;
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
add(y,x,c);
}
tp(S);
printf("%d\n",dp[S][K]);
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值