zoukankan      html  css  js  c++  java
  • HDU 1561 The more, The Better

    The more, The Better

    Time Limit: 2000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1561
    64-bit integer IO format: %I64d      Java class name: Main
     
    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗?
     

    Input

    每个测试实例首先包括2个整数,N,M.(1 <= M <= N <= 200);在接下来的N行里,每行包括2个整数,a,b. 在第 i 行,a 代表要攻克第 i 个城堡必须先攻克第 a 个城堡,如果 a = 0 则代表可以直接攻克第 i 个城堡。b 代表第 i 个城堡的宝物数量, b >= 0。当N = 0, M = 0输入结束。
     

    Output

    对于每个测试实例,输出一个整数,代表ACboy攻克M个城堡所获得的最多宝物的数量。
     

    Sample Input

    3 2
    0 1
    0 2
    0 3
    7 4
    2 2
    0 1
    0 4
    2 1
    7 1
    7 6
    2 2
    0 0

    Sample Output

    5
    13

    Source

     
    解题:依赖背包。。。转成树形dp
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 210;
    18 struct arc{
    19     int to,next;
    20     arc(int x = 0,int y = -1){
    21         to = x;
    22         next = y;
    23     }
    24 };
    25 arc e[maxn*maxn];
    26 int dp[maxn][maxn],head[maxn],val[maxn],n,m,tot;
    27 bool vis[maxn];
    28 void add(int u,int v){
    29     e[tot] = arc(v,head[u]);
    30     head[u] = tot++;
    31 }
    32 void dfs(int u){
    33     vis[u] = true;
    34     dp[u][1] = val[u];
    35     for(int i = head[u]; ~i; i = e[i].next){
    36         if(vis[e[i].to]) continue;
    37         dfs(e[i].to);
    38         for(int j = m+1; j >= 1; j--){
    39             for(int k = 1; k < j; ++k)
    40                 dp[u][j] = max(dp[u][j],dp[u][j-k] + dp[e[i].to][k]);
    41         }
    42     }
    43 }
    44 int main() {
    45     int u,v;
    46     while(scanf("%d %d",&n,&m),n||m){
    47         memset(head,-1,sizeof(head));
    48         memset(vis,false,sizeof(vis));
    49         for(int i = 1; i <= n; ++i){
    50             scanf("%d %d",&u,val+i);
    51             add(u,i);
    52         }
    53         memset(dp,0,sizeof(dp));
    54         dfs(0);
    55         printf("%d
    ",dp[0][m+1]);
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    返回一个整数数组中子数组的最大值
    软工概论第二周个人项目四则运算二(改进)
    构建之法阅读笔记01
    单例模式
    .net基础加强
    使用jquery easy ui
    抽象工厂类
    System.Linq.Expressions.Expression
    创建上下文对象
    DBSesson
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4108102.html
Copyright © 2011-2022 走看看