zoukankan      html  css  js  c++  java
  • HDU4003Find Metal Mineral[树形DP 分组背包]

    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


    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

    终于填坑了
    和选课一样,树形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[N],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[N][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
    ",f[s][m]);
        }
        return 0;
    }
  • 相关阅读:
    阿里开源混沌工程工具 ChaosBlade
    十天入门java教程 Day01
    如何破解IDEA
    Locust压力测试使用总结
    python+requests接口自动化测试框架实例详解教程
    一个完整的性能测试流程
    jmeter测试报告汉化及脚本编写
    tomcat的日志文件权限与启动用户的权限不一致
    Linux下部署开源版“禅道”项目管理系统
    ELK原理与介绍
  • 原文地址:https://www.cnblogs.com/candy99/p/5899632.html
Copyright © 2011-2022 走看看