D - Find Metal Mineral 树上的分组背包dp
2013-10-07 12:46
232 查看
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
***************************************************************************************************************************
给一棵带权的树,root上有n个人,问遍历完所有节点所花费的代价
***************************************************************************************************************************
View Code
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
***************************************************************************************************************************
给一棵带权的树,root上有n个人,问遍历完所有节点所花费的代价
***************************************************************************************************************************
/* 本题重在分析,dp[son][j]表示以i为root的树, root中有j个机器人分到son,其中j=0时表示robot 返回root时花费,其他表示不返回。 该题思路,是每次root给子树j个robots,然后subtree往下推 枚举每一种情况,记录极优值, */ #include<iostream> #include<string> #include<cstring> #include<queue> #include<cstdio> #include<cctype> #include<cmath> using namespace std; int dp[10014][15]; struct ed { int v,len,next; }e[20011]; int head[10011],e_num,x,y,value; int i,j,n,m,k; void add(int x,int y,int value) { e[e_num].v=y; e[e_num].len=value; e[e_num].next=head[x]; head[x]=e_num++; } void dfs(int root,int p) { for(int it=head[root];it!=-1;it=e[it].next) { int cur=e[it].v; if(cur==p)//双向 continue; dfs(cur,root); for(int jt=k;jt>=0;jt--) { dp[root][jt]+=dp[cur][0]+2*e[it].len;//root中有jt个机器人,如果转一圈返回时的代价 for(int kt=1;kt<=jt;kt++) dp[root][jt]=min(dp[root][jt],dp[root][jt-kt]+dp[cur][kt]+kt*e[it].len);//极优值 } } } int main() { while(~scanf("%d%d%d",&n,&m,&k)) { e_num=0; memset(head,-1,sizeof(head)); memset(dp,0,sizeof(dp)); for(i=0;i<n-1;i++) { scanf("%d%d%d",&x,&y,&value); add(x,y,value); add(y,x,value); } dfs(m,-1); printf("%d\n",dp[m][k]); } return 0; }
View Code
相关文章推荐
- hdu1561 树上分组背包
- 51nod 1007 正整数分组(背包/dp)
- HDU4003 树上的分组背包
- 【HDU5534 2015长春赛区H】【树上DP转完全背包】Partial Tree 不同度不同权对应最大树权
- 【vjios1488】【分组背包+dp优化】路灯改建计划
- HDU4003Find Metal Mineral[树形DP 分组背包]
- ZOJ 3201 Tree of Tree(树形dp + 分组背包)
- POJ 2468 Apple Tree 树上瞎搞分组背包
- HDU3692--least common multiple(数论优化+分组背包);DP--数论
- hdu3033(分组背包二次dp处理)
- dp之分组背包hdu3033 最少取1次的解法(推荐)
- dp之分组背包hdu3535(推荐)
- [树上依赖多重背包 DP] BZOJ 4910 [Sdoi2017]苹果树
- DP(分组背包两种二进制优化) Problem S:Coins(HDU 2844)
- hdu5148 树形dp,分组背包
- HDU 4003 Find Metal Minaral 树上瞎搞分组背包
- Hihocoder 1055 树形DP(分组背包)
- HDU 4276 树形dp+spfa+分组背包
- DP复习——分组背包
- hdu1712---ACboy needs your help(dp,分组背包)