zoukankan      html  css  js  c++  java
  • 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;
     

    题意:

          

    题解:

        我们设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=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //****************************************
    
    
    const int N=500000+100;
    const ll inf = 1ll<<61;
    const int mod= 1000000007;
    
    vector< pair<int ,int> >G[N];
    int dp[N][11],n,S,K,u,v,w;
    void dfs(int x,int pre) {
        for(int i=0;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>=0;k--) {
                dp[x][k]+=dp[son][0]+cost*2;
                for(int y=1;y<=k;y++) {
                    dp[x][k]=min(dp[x][k],dp[x][k-y]+dp[son][y]+cost*y);
                }
            }
        }
    }
    void init() {
    
        for(int i=0;i<=n;i++) G[i].clear();
        mem(dp);
    }
    int main() {
        while(~(scanf("%d%d%d",&n,&S,&K))) {
                init();
            for(int i=1;i<n;i+=1) {
            scanf("%d%d%d",&u,&v,&w);
            G[u].pb(MP(v,w));
            G[v].pb(MP(u,w));
        }
        dfs(S,-1);
        cout<<dp[S][K]<<endl;
        }
    
        return 0;
    }
    daima
  • 相关阅读:
    Android启动过程介绍
    第九届蓝桥杯Java B组
    二分模板和区别
    第十届蓝桥杯Java B组
    2019 ICPC Asia Nanjing Regional(现场赛)
    PAT 甲级 1057 Stack
    P1972 [SDOI2009]HH的项链
    Python中的字典排序
    Comet OJ
    ekmp模板
  • 原文地址:https://www.cnblogs.com/zxhl/p/5030703.html
Copyright © 2011-2022 走看看