• D


    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
    ***************************************************************************************************************************
    给一棵带权的树,root上有n个人,问遍历完所有节点所花费的代价
    ***************************************************************************************************************************
     1 /*
     2 本题重在分析,dp[son][j]表示以i为root的树,
     3 root中有j个机器人分到son,其中j=0时表示robot
     4 返回root时花费,其他表示不返回。
     5 该题思路,是每次root给子树j个robots,然后subtree往下推
     6 枚举每一种情况,记录极优值,
     7 */
     8 #include<iostream>
     9 #include<string>
    10 #include<cstring>
    11 #include<queue>
    12 #include<cstdio>
    13 #include<cctype>
    14 #include<cmath>
    15 using namespace std;
    16 int dp[10014][15];
    17 struct ed
    18 {
    19     int v,len,next;
    20 }e[20011];
    21 int head[10011],e_num,x,y,value;
    22 int i,j,n,m,k;
    23 void add(int x,int y,int value)
    24 {
    25     e[e_num].v=y;
    26     e[e_num].len=value;
    27     e[e_num].next=head[x];
    28     head[x]=e_num++;
    29 }
    30 void dfs(int root,int p)
    31 {
    32     for(int it=head[root];it!=-1;it=e[it].next)
    33     {
    34         int cur=e[it].v;
    35         if(cur==p)//双向
    36           continue;
    37         dfs(cur,root);
    38         for(int jt=k;jt>=0;jt--)
    39         {
    40             dp[root][jt]+=dp[cur][0]+2*e[it].len;//root中有jt个机器人,如果转一圈返回时的代价
    41             for(int kt=1;kt<=jt;kt++)
    42               dp[root][jt]=min(dp[root][jt],dp[root][jt-kt]+dp[cur][kt]+kt*e[it].len);//极优值
    43         }
    44     }
    45 }
    46 int main()
    47 {
    48     while(~scanf("%d%d%d",&n,&m,&k))
    49     {
    50         e_num=0;
    51         memset(head,-1,sizeof(head));
    52         memset(dp,0,sizeof(dp));
    53         for(i=0;i<n-1;i++)
    54         {
    55             scanf("%d%d%d",&x,&y,&value);
    56             add(x,y,value);
    57             add(y,x,value);
    58         }
    59         dfs(m,-1);
    60         printf("%d
    ",dp[m][k]);
    61 
    62    }
    63     return 0;
    64 
    65 }
    View Code
     1 /*
     2 本题重在分析,dp[son][j]表示以i为root的树,
     3 root中有j个机器人分到son,其中j=0时表示robot
     4 返回root时花费,其他表示不返回。
     5 该题思路,是每次root给子树j个robots,然后subtree往下推
     6 枚举每一种情况,记录极优值,
     7 */
     8 #include<iostream>
     9 #include<string>
    10 #include<cstring>
    11 #include<queue>
    12 #include<cstdio>
    13 #include<cctype>
    14 #include<cmath>
    15 using namespace std;
    16 int dp[10014][15];
    17 struct ed
    18 {
    19     int v,len,next;
    20 }e[20011];
    21 int head[10011],e_num,x,y,value;
    22 int i,j,n,m,k;
    23 void add(int x,int y,int value)
    24 {
    25     e[e_num].v=y;
    26     e[e_num].len=value;
    27     e[e_num].next=head[x];
    28     head[x]=e_num++;
    29 }
    30 void dfs(int root,int p)
    31 {
    32     for(int it=head[root];it!=-1;it=e[it].next)
    33     {
    34         int cur=e[it].v;
    35         if(cur==p)//双向
    36           continue;
    37         dfs(cur,root);
    38         for(int jt=k;jt>=0;jt--)
    39         {
    40             dp[root][jt]+=dp[cur][0]+2*e[it].len;//root中有jt个机器人,如果转一圈返回时的代价
    41             for(int kt=1;kt<=jt;kt++)
    42               dp[root][jt]=min(dp[root][jt],dp[root][jt-kt]+dp[cur][kt]+kt*e[it].len);//极优值
    43         }
    44     }
    45 }
    46 int main()
    47 {
    48     while(~scanf("%d%d%d",&n,&m,&k))
    49     {
    50         e_num=0;
    51         memset(head,-1,sizeof(head));
    52         memset(dp,0,sizeof(dp));
    53         for(i=0;i<n-1;i++)
    54         {
    55             scanf("%d%d%d",&x,&y,&value);
    56             add(x,y,value);
    57             add(y,x,value);
    58         }
    59         dfs(m,-1);
    60         printf("%d
    ",dp[m][k]);
    61 
    62    }
    63     return 0;
    64 
    65 }
    View Code
  • 相关阅读:
    被学长教会的高斯消元法Gauss
    KMP字符串匹配算法翔解❤
    fkwの题目(祝松松生日快乐!)
    NOI-linux下VIM的个人常用配置
    从2017年暑假到现在手打的模板↑_↑
    【テンプレート】初级数据结构
    【テンプレート】高精
    DP(第二版)
    luogu P1029 最大公约数和最小公倍数问题
    贪心题整理
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3355280.html
Copyright © 2020-2023  润新知