zoukankan      html  css  js  c++  java
  • Treasure Hunt I 树形dp ,求出树上的背包

    Description

    Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds a treasure map, and he wants to get as much as possible.

    Akiba consists of n towns and n-1 roads. There is a way from each town to any other. Each town contains some treasure values Vi. CC starts from town k(his hometown), at day 0. After m days, the bloodsucker will appear and CC would be killed if he hasn't been back yet, it means CC has m days for hunting the treasure at most. It takes CC Ti days to move from one town to another neighbour town.(Two towns called neighbour if they are the endpoint of one road.) You can assume CC will get the treasure immediately as he arrives at that town. CC wants to obtain as much value as possible, keeping him alive at the same time.

    Input

    There are multiple cases, about 50 cases.
    The first line of each case contains an integer n, indicating there are n towns.
    The following line describe the treasure's value in each town. "V1 V2 ... Vn". Vi is the value of the treasure in ith town. Each value is separated by one blank.
    The next n-1 lines describe the n-1 roads in Akiba. "i j Ti" Means the ith town and the jth town are endpoints of that road. It takes Ti days to get through this road.
    The last line has two integer k and m as described above.

    1<=n<=100, 0<=Vi<=1000 , 1<=Ti<=10
    1<=k<=n, 1<=m<=200
    All the inputs are integers.

    Output

    Just output the max value CC can get, and you should keep CC alive after m days.

    Sample Input

    2
    1 3
    1 2 1
    1 2
    2
    1 3
    2 1 1
    2 1
    2
    3 3
    1 2 1
    2 5

    Sample Output

    4
    3
    6
    ***********************************************************************************************************************************************************
    树上的dp
    ***********************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<queue>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 bool vis[1001];
     9 int dp[110][220];
    10 typedef pair<int,int>PII;
    11 vector<PII>adj[110];
    12 int val[1001],n,m,k;
    13 void dfs(int u)//树形dp
    14 {
    15     int it,jt,kt;
    16   vis[u]=true;
    17   for(it=0;it<=m;it++)
    18    dp[u][it]=val[u];
    19   for(it=0;it<adj[u].size();it++)
    20   {
    21       int v=adj[u][it].first;
    22       int w=adj[u][it].second;
    23       if(vis[v]==true)continue;
    24       dfs(v);
    25       for(jt=m;jt>=0;jt--)
    26        for(kt=2*w;kt<=jt;kt++)
    27        {
    28          dp[u][jt]=max(dp[u][jt],dp[u][jt-kt]+dp[v][kt-2*w]);
    29        }
    30   }
    31 }
    32 int main()
    33 {
    34     int v,u,w;
    35     while(~scanf("%d",&n))
    36     {
    37         for(int i=0;i<=n;i++)
    38           adj[i].clear();
    39         for(int i=1;i<=n;i++)
    40          scanf("%d",&val[i]);
    41         for(int i=1;i<n;i++)
    42         {
    43            scanf("%d %d %d",&u,&v,&w);
    44            adj[u].push_back(make_pair(v,w));
    45            adj[v].push_back(make_pair(u,w));
    46         }
    47         scanf("%d %d",&k,&m);
    48         memset(vis,false,sizeof(vis));
    49         memset(dp,0,sizeof(dp));
    50         dfs(k);
    51         printf("%d
    ",dp[k][m]);
    52     }
    53 }
    View Code
  • 相关阅读:
    JAVA泛类型(汽车Demo)
    java自定义事件机制分析
    ExtJS自定义事件
    模块化设计进化
    面向服务的SOA架构与服务总线ESB
    数据加密数字签名
    面试题
    一点ExtJS开发的感悟
    学习代理模式
    抽象类与接口
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3439165.html
Copyright © 2011-2022 走看看