HDU4003 Find Metal Mineral 树形DP

Find Metal Mineral

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;

 

题意:

HDU4003 Find Metal Mineral  树形DP

题解:

我们设dp[i][j] 表示已i为根节点,放j个机器人的最小话费

那么:

dp(i,j)=(dp(son,0)+cast[son]*2)(子树没有停留机器人)+min(dp[p][m-y]+y*cost[son]+dp[son][y])(子树停留了y个机器人)

//meek
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; vector< pair<int ,int> >G[N];
int dp[N][],n,S,K,u,v,w;
void dfs(int x,int pre) {
for(int i=;i<G[x].size();i++) {
if(G[x][i].fi==pre) continue;
int son=G[x][i].fi,cost=G[x][i].se;
dfs(G[x][i].fi,x);
for(int k=K;k>=;k--) {
dp[x][k]+=dp[son][]+cost*;
for(int y=;y<=k;y++) {
dp[x][k]=min(dp[x][k],dp[x][k-y]+dp[son][y]+cost*y);
}
}
}
}
void init() { for(int i=;i<=n;i++) G[i].clear();
mem(dp);
}
int main() {
while(~(scanf("%d%d%d",&n,&S,&K))) {
init();
for(int i=;i<n;i+=) {
scanf("%d%d%d",&u,&v,&w);
G[u].pb(MP(v,w));
G[v].pb(MP(u,w));
}
dfs(S,-);
cout<<dp[S][K]<<endl;
} return ;
}

daima

上一篇:hdu 4003 Find Metal Mineral 树形DP


下一篇:024 Android 自定义样式对话框(AlertDialog)