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 }
  • 相关阅读:
    Codeforces Gym101502 K.Malek and Summer Semester
    Codeforces Gym101502 J-取数博弈
    Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)
    Codeforces Gym101502 H.Eyad and Math-换底公式
    Codeforces Gym101502 F.Building Numbers-前缀和
    Codeforces Gym101502 B.Linear Algebra Test-STL(map)
    Codeforces Gym101502 A.Very Hard Question
    bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
    Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
    Codeforces Round #250 (Div. 2) A, B, C
  • 原文地址:https://www.cnblogs.com/wsy107316/p/13425438.html
Copyright © 2011-2022 走看看