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.
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
32HintIn 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;}
阅读全文
0 0
- Hdu 4003 Find Metal Mineral 树型背包DP
- hdu 4003 Find Metal Mineral (树形背包dp)
- hdu 4003 Find Metal Mineral (树形dp+分组背包)
- hdu 4003 Find Metal Mineral 【树形dp,分组背包】
- hdu 4003 Find Metal Mineral(树形DP+分组背包)
- hdu 4003 Find Metal Mineral 树形dp+分组背包
- hdu 4003 Find Metal Mineral (树形dp+背包)
- hdu 4003 Find Metal Mineral(树形dp+分组背包)
- HDU 4003 Find Metal Mineral(树形dp + 分组背包)
- hdu 4003 Find Metal Mineral(树形dp+分组背包)
- HDU 4003 Find Metal Mineral (树形DP+分组背包)
- HDU 4003 Find Metal Mineral【经典树形DP背包】
- Find Metal Mineral - HDU 4003 dp
- HDU 4003 Find Metal Mineral 树形DP
- hdu 4003 Find Metal Mineral 树形DP
- HDU-4003 Find Metal Mineral 树形dp
- hdu 4003 Find Metal Mineral(树形DP+分组背包,每个物品必须只能选一次)
- hdu 4003 Find Metal Mineral 树形DP:树形背包 ★ 好题
- 凸包模板
- oracle单引号和双引号的区别
- 接口与多态
- Truck History(最小生成树)
- 自己初步了解python数据爬虫
- Hdu 4003 Find Metal Mineral 树型背包DP
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
- 找规律 permutation
- UVA 442 栈解析表达式
- oracle(四)
- Java反射
- SQL ORDER BY 子句
- CodeForces
- POJ 2503 Babelfish(STL、二分、字典树、哈希)