Hdu 4003 tree-like dp + grouping backpack

Source: Internet
Author: User

Find Metal MineralTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission (s): 2106 Accepted Submission (s): 953


Problem DescriptionHumans 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 shocould 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 shocould make a optimal plan which cost minimal energy cost.
InputThere 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 shoshould cost w.
1 <= N <= 10000, 1 <= S <= N, 1 <= k <= 10, 1 <= x, y <= N, 1 <= w <= 10000.
OutputFor each cases output one line with the minimal energy cost.
Sample Input

3 1 11 2 11 3 13 1 21 2 11 3 1

Sample Output
32HintIn the first case: 1->2->1->3 the cost is 3;In the second case: 1->2; 1->3 the cost is 2; 


Given a tree, starting point, number of robots, the minimum path and number of robots to traverse all nodes.

Ddp [u] [I] indicates the number of robots in the sub-tree with u as the root node after access. If there are 0 robots, it must have been returned after a robot completes access, therefore, special considerations are required,

Code:

/* ***********************************************Author :xianxingwuguanCreated Time :2014-2-6 16:15:10File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include 
 
  #include 
  
   #include #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include 
             using namespace std;#define INF 1000000#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=40009;int head[maxn],tol;struct node{ int next,to,val; node(){}; node(int _next,int _to,int _val):next(_next),to(_to),val(_val){}}edge[5*maxn];void add(int u,int v,int val){ edge[tol]=node(head[u],v,val); head[u]=tol++;}int K,dp[maxn][13];void dfs(int u,int fa){for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==fa)continue;dfs(v,u);for(int j=K;j>=0;j--){dp[u][j]+=dp[v][0]+2*edge[i].val;for(int k=0;k<=j;k++)dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]+k*edge[i].val);}}}int main(){int i,j,k,m,n,s; while(~scanf("%d%d%d",&n,&s,&K)){memset(head,-1,sizeof(head));tol=0;for(i=1;i
              
               

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.