zoukankan      html  css  js  c++  java
  • 01背包

    测试地址:here

     

     AC_Code:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 typedef unsigned long long ull;
     5 const int maxn = 1010;
     6 const int mod = 2333333;
     7 const int inf = 0x3f3f3f3f;
     8 #define gok ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
     9 #define pii pair<int,int>
    10 #define fi first
    11 #define se second
    12 #define pb push_back
    13 #define rep(i,first,second) for(ll i=first;i<=second;i++)
    14 #define dep(i,first,second) for(ll i=first;i>=second;i--)
    15 #define erep(i,u) for(ll i=head[u];~i;i=e[i].nxt)
    16 //0-1背包
    17 /*
    18 f[i][j]只看前i个物品,总体积是j的情况下,总价值最大是多少
    19 ans = max(f[n][0~v]);
    20 
    21 1.f[i][j] = f[i-1][j];        //不选
    22 2.f[i][j] = f[i-1][j-v[i]];   //选
    23 f[i][j] = max(1,2);
    24 
    25 初始化f[0][0]=0;
    26 O(N*V)
    27 */
    28 int n,m;
    29 int dp[maxn][maxn];
    30 int v[maxn],w[maxn];
    31 
    32 int main()
    33 {
    34     gok;
    35     cin>>n>>m;
    36     rep(i,1,n) cin>>v[i]>>w[i];
    37     rep(i,1,n){
    38         rep(j,0,m){
    39             dp[i][j]=dp[i-1][j];
    40             if( j>=v[i] ){
    41                 dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+w[i]);
    42             }
    43         }
    44     }
    45     int res=0;
    46     rep(i,1,m) res=max(res,dp[n][i]);
    47     cout<<res<<'
    ';
    48     return 0;
    49 }

    优化版AC_Code:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 typedef unsigned long long ull;
     5 const int maxn = 1010;
     6 const int mod = 2333333;
     7 const int inf = 0x3f3f3f3f;
     8 #define gok ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
     9 #define pii pair<int,int>
    10 #define fi first
    11 #define se second
    12 #define pb push_back
    13 #define rep(i,first,second) for(ll i=first;i<=second;i++)
    14 #define dep(i,first,second) for(ll i=first;i>=second;i--)
    15 #define erep(i,u) for(ll i=head[u];~i;i=e[i].nxt)
    16 //0-1背包
    17 /*
    18 f[i][j]只看前i个物品,总体积是j的情况下,总价值最大是多少
    19 ans = max(f[n][0~v]);
    20 
    21 1.f[i][j] = f[i-1][j];        //不选
    22 2.f[i][j] = f[i-1][j-v[i]];   //选
    23 f[i][j] = max(1,2);
    24 
    25 初始化f[0][0]=0;
    26 O(N*V)
    27 */
    28 
    29 /*
    30 f[i]的状态只与f[i-1]有关,所以用滚动数组
    31 
    32 */
    33 int n,m;
    34 int dp[maxn];
    35 int v[maxn],w[maxn];
    36 
    37 int main()
    38 {
    39     gok;
    40     cin>>n>>m;
    41     rep(i,1,n) cin>>v[i]>>w[i];
    42     rep(i,1,n){
    43         dep(j,m,v[i]){//要保证dp[j-v[i]],用的是i-1的,所以倒着,否则j-v[i]在j之前已经算过,那么更新的时候用的就是dp[i][j-v[i]],而不是原来dp[i-1][j-v[i]];
    44             dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
    45         }
    46     }
    47     cout<<dp[m]<<'
    ';
    48 
    49     return 0;
    50 }
  • 相关阅读:
    error MSB6006: ”cmd.exe” exited with code 1
    OpenGL简介
    OSG例程(1) 交互(Pick)
    $err,hr
    [转载]操作数的寻址方式
    严重推荐的图形学讲义
    编译通过,运行时osgDB::ReadImageFile()出错 d和非d的lib
    空间变换的顺序SRT
    OSG例程(3) 利用更新回调制作路径动画
    Visitor模式的C++实现
  • 原文地址:https://www.cnblogs.com/wsy107316/p/13425438.html
Copyright © 2011-2022 走看看