zoukankan      html  css  js  c++  java
  • hdu4003 Find Metal Mineral

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 2996    Accepted Submission(s): 1377


    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[i][j]表示节点i派j个人走完i节点子树范围内的所有节点的最小代价。
    其中j=0时,dp[i][j]表示用一个机器人去走完所有子树,最后又回到i这个节点所要的费用,j>0时表示用j个机器人走完所有子树要用的费用,然后分组背包就行了。


    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 0x7fffffff
    #define maxn 10050
    int first[maxn];
    struct node{
        int to,next,w;
    }e[2*maxn];
    int dp[maxn][15],vis[maxn];
    int n,k;
    
    void dfs(int u)
    {
        int i,j,v,l;
        vis[u]=1;
        for(i=first[u];i!=-1;i=e[i].next){
            v=e[i].to;
            if(vis[v])continue;
            dfs(v);
            for(j=k;j>=0;j--){
                dp[u][j]+=dp[v][0]+2*e[i].w; //这里从父亲节点到子节点要加上两倍的路代价
                for(l=1;l<=j;l++){           //这里用分组背包看看是不是有更好的替代0的方案
                    dp[u][j]=min(dp[u][j],dp[u][j-l]+dp[v][l]+l*e[i].w  );
                }
            }
        }
    }
    int main()
    {
        int m,i,j,s,f,tot,c,d;
        while(scanf("%d%d%d",&n,&s,&k)!=EOF)
        {
            tot=0;
            memset(first,-1,sizeof(first));
            for(i=1;i<=n-1;i++){
                scanf("%d%d%d",&c,&d,&f);
                tot++;
                e[tot].to=d;e[tot].next=first[c];e[tot].w=f;
                first[c]=tot;
    
                tot++;
                e[tot].to=c;e[tot].next=first[d];e[tot].w=f;
                first[d]=tot;
    
            }
            memset(dp,0,sizeof(dp));
            memset(vis,0,sizeof(vis));
            dfs(s);
            printf("%d
    ",dp[s][k]);
        }
        return 0;
    }
    





  • 相关阅读:
    转载:Background Worker in .NET 2.0
    转载:WPF 3.5 SP1 Feature: BindingGroups with Itemlevel Validation
    转载:NonLive Scrolling延时滚动数据
    俺的机器上VS的MenuBar的名称列表
    动态控件的状态问题的分析 概括
    基于插件的权限系统构想
    《ASP.NET组件设计》没提到的一个类
    有关集中用户的问题
    SQL Server日志清空方法
    ADO事务处理方式运行正常
  • 原文地址:https://www.cnblogs.com/herumw/p/9464623.html
Copyright © 2011-2022 走看看