• 2016huasacm暑假集训训练四 DP_B


    题目链接:https://vjudge.net/contest/125308#problem/M

    题意:有N件物品和一个容量为V的背包。第i件物品的费用是体积c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。简单的01背包问题主要是状态转移式

    f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]},即可求解:

    AC代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int dp[1005];
     6 struct Node
     7 {
     8     int volume;
     9     int value;
    10 } a[1005];
    11 int main()
    12 {
    13     int t1,i, j, t, m;
    14     scanf("%d",&t1);
    15     while(t1--)
    16     {
    17         scanf("%d%d", &t, &m);
    18         memset(dp,0,sizeof(dp));
    19         for (i = 1; i <= t; i++)
    20             scanf("%d",&a[i].value);
    21             for(i  = 1; i <= t; i ++)
    22                 scanf("%d",&a[i].volume);
    23         for (i = 1; i <= t; i++)
    24         {
    25             for (j = m; j >= a[i].volume; j--)
    26             {
    27                 dp[j] = max(dp[j], dp[j - a[i].volume] + a[i].value);
    28             }
    29         }
    30         printf("%d
    ", dp[m]);
    31     }
    32     return 0;
    33 }
    34 FAQ | About | Google Group | Discuss | Github | Author 
    35 All Copyright Reserved ©2010-2016 HUST ACM/ICPC TEAM 
  • 相关阅读:
    全排列(next_permutation)
    Codeforces Round #321 (Div. 2)C(tree dfs)
    cf_ducational Codeforces Round 16_D(gcd)
    cf455a(简单dp)
    cf584a(水题)
    cf112a(水题)
    冒泡排序
    Python内置类型性能分析
    常见时间复杂度
    MongoDB 备份
  • 原文地址:https://www.cnblogs.com/LIUWEI123/p/5743590.html
Copyright © 2020-2023  润新知