HDU4003Find Metal Mineral[树形DP 分组背包]
2016-09-23 14:04
330 查看
Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3397 Accepted Submission(s): 1588
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each cases output one line with the minimal energy cost.
[align=left]Sample Input[/align]
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
[align=left]Sample Output[/align]
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;
[align=left]Source[/align]
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
终于填坑了
和选课一样,树形DP,每个子节点是一个分组,不同的组是放的机器人不同
f[i][j]表示子树i放j个机器人(不用上来)的最小代价
f[i][0]表示放一个又上来 可以发现放多个有上来的不可能更优
因为每组至少选一个,所以先把f[v][0]放上
// // main.cpp // hdu4003 // // Created by Candy on 9/23/16. // Copyright © 2016 Candy. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=1e4+5,M=12; int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } struct edge{ int v,w,ne; }e[N<<1]; int h ,cnt=0; void ins(int u,int v,int w){ cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt; } int n,s,m,u,v,w; int f [M]; void dp(int u,int fa){ for(int i=h[u];i;i=e[i].ne){ int v=e[i].v,w=e[i].w; if(v==fa) continue; dp(v,u); for(int j=m;j>=0;j--){ //ti ji f[u][j]+=f[v][0]+w*2;//must choose for(int k=1;k<=j;k++)//group f[u][j]=min(f[u][j],f[u][j-k]+f[v][k]+w*k); } } } int main(int argc, const char * argv[]) { while(cin>>n>>s>>m){ cnt=0; memset(h,0,sizeof(h)); for(int i=1;i<=n-1;i++){ u=read();v=read();w=read(); ins(u,v,w); } memset(f,0,sizeof(f)); dp(s,0); printf("%d\n",f[s][m]); } return 0; }
相关文章推荐
- HDU 4003 Find Metal Mineral(分组背包+树形DP)
- hdu 5148 树形dp,分组背包
- 分组背包+树形DP(BY LPX)
- hdu5148 树形dp,分组背包
- HDU 4003 (树形DP + 分组背包)
- hdu1561 The more, The Better(常见分组背包+树形dp)
- ZOJ 3201 Tree of Tree(树形dp + 分组背包)
- 树形DP+(分组背包||二叉树,一般树,森林之间的转换)codevs 1378 选课
- 树形DP+背包(poj1155泛化分组背包)
- (中等) 树形dp(分组背包) POJ 3345 Bribing FIPA
- Hihocoder 1055 树形DP(分组背包)
- 树形DP+背包(poj1155泛化分组背包)
- 【POJ1947】Rebuilding Roads,树形DP(本文分组背包做法)
- HDU 4276 树形dp+spfa+分组背包
- poj1947 ----- 树形DP - 分组背包做法
- hdu 4276 The Ghost Blows Light(树形DP+最短路+分组背包)好题。。。
- UVA Live Archive 4015 Cave (树形dp,分组背包)
- 【树形DP】【分组背包】【HDU1561】
- hdu 4003 Find Metal Mineral 【树形dp,分组背包】
- POJ 1947 树形DP(分组背包)