zoukankan      html  css  js  c++  java
  • Acwing-----算法基础课之第五讲动态规划(一)

    2. 01背包问题

    // 二维解法
    #include <iostream>
    using namespace std;
    
    const int N = 1010;
    
    int n, m, v[N], w[N], f[N][N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
        f[0][0] = 0;
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= m; ++j) {
                f[i][j] = f[i - 1][j];
                if (j >= v[i]) f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
            }
        }
        cout << f[n][m] << endl;
        return 0;
    }
    
    //优化解法
    #include <iostream>
    using namespace std;
    
    const int N = 1010;
    
    int n, m, v[N], w[N], f[N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
    
        for (int i = 1; i <= n; ++i) {
            for (int j = m; j >= v[i]; --j) {
                f[j] = max(f[j], f[j - v[i]] + w[i]);
            }
        }
        cout << f[m] << endl;
        return 0;
    }
    

    3. 完全背包问题

    //二维解法
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 1010;
    
    int n, m, v[N], w[N], f[N][N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= m; ++j) {
                f[i][j] = f[i - 1][j];
                if (j >= v[i]) f[i][j] = max(f[i][j], f[i][j - v[i]] + w[i]);
            }
        }
        cout << f[n][m] << endl;
        return 0;
    }
    
    //优化解法
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 1010;
    
    int n, m, v[N], w[N], f[N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
        
        for (int i = 1; i <= n; ++i) {
            for (int j = v[i]; j <= m; ++j) {
                f[j] = max(f[j], f[j - v[i]] + w[i]);
            }
        }
        cout << f[m] << endl;
        return 0;
    }
    

    4. 多重背包问题 I

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 110;
    int n, m;
    int v[N], w[N], s[N];
    int f[N][N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i] >> s[i];
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= m; ++j) {
                for (int k = 0; k <= s[i] && k * v[i] <= j; ++k) {
                    f[i][j] = max(f[i][j], f[i - 1][j - k * v[i]] + w[i] * k);
                }
            }
        }
        
        cout  << f[n][m] << endl;
        
        return 0;
    }
    

    5. 多重背包问题 II

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int N = 25000, M = 2010;
    int n, m, v[N], w[N], f[N];
    
    int main() {
        cin >> n >> m;
        int cnt = 0;
        for (int i = 1; i <= n; ++i) {
            int a, b, s;
            cin >> a >> b >> s;
            int k = 1;
            while (k <= s) {
                cnt ++;
                v[cnt] = a * k;
                w[cnt] = b * k;
                s -= k;
                k *= 2;
            }
            if (s > 0) {
                cnt++;
                v[cnt] = a * s;
                w[cnt] = b * s;
            }
        }
        n = cnt;
        for (int i = 1; i <= n; ++i) {
            for (int j = m; j >= v[i]; --j) {
                f[j] = max(f[j], f[j - v[i]] + w[i]);
            }
        }
        cout << f[m] << endl;
        return 0;
    }
    

    9. 分组背包问题

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int N = 110;
    int n, m, v[N][N], w[N][N], f[N], s[N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            cin >> s[i];
            for (int j = 0; j < s[i]; ++j) cin >> v[i][j] >> w[i][j];
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = m; j >= 0; --j) {
                for (int k = 0; k < s[i]; ++k) {
                    if (v[i][k] <= j) f[j] = max(f[j], f[j- v[i][k]] + w[i][k]);
                }
            }
        }
        cout << f[m] << endl;
        return 0;
    }
    
  • 相关阅读:
    2020年Android面试题含答案
    flutter系列(一)----- 开发环境搭建
    Android应用安全防护和逆向分析 ——apk混淆成其他语言代码
    Android应用安全防护和逆向分析 ——apk反编译
    Android中 TextView 加载 混合字符 自动换行解决方案
    H5跳转app本地的规则定义
    Android ListView 九大重要属性详细分析
    ListView和ScrollView滑动到顶部
    简要的汇总Android
    关于ViewPager+Fragment中Fragment不销毁/生命周期
  • 原文地址:https://www.cnblogs.com/clown9804/p/13783046.html
Copyright © 2011-2022 走看看