HDU 4003 Find Metal Mineral

3744 ワード

ツリーDP+リュック.
dp[u][i]=sum(dp[v][0]+2*w)    //初期化
dp[u][i]=min(dp[u][i-k]+dp[v][k]+k*w)   k>=1;
Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2352    Accepted Submission(s): 1069
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
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 
Recommend
lcy
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define ll long long
#define prt(k) cout<<#k"="<<k<<" "

const int N=20020;
#include<vector>
vector<int> g[N];
int n,root,K;   ///K<=10
int d[N],to[N],head[N],next[N];
int tot;
void add(int u,int v,int w)
{
    d[++tot]=w;
    to[tot]=v;
    next[tot]=head[u];
    head[u]=tot;
}
ll dp[N][22];
ll dfs(int u,int fa)
{
    for(int e=head[u];~e;e=next[e])
    {
        int v=to[e],w=d[e];
        if(v==fa) continue;
        dfs(v,u);
        for(int i=K;i>=0;i--)
        {
            dp[u][i]+=dp[v][0]+2*w;
            for(int j=1;j<=i;j++)
                dp[u][i]=min(dp[u][i],dp[u][i-j]+dp[v][j]+j*w);
        }
    }
}

int main()
{
    while(cin>>n>>root>>K)
    {
        memset(head,-1,sizeof head); tot=0;
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w); add(v,u,w);
        }
        memset(dp,0,sizeof dp);
        dfs(root,-1);
        printf("%d
",dp[root][K]); } }