zoukankan      html  css  js  c++  java
  • 洛谷 1064 金明的预算方案

    金明的预算方案——一道传说时依赖背包的题目,然而今天看了一眼发现其实分组背包就可以AC的。

    导致此题难度下降的一个重要因素就是附件太少了!于是这个题最方便的做法就是重置物品,跑分组背包。

    AC代码:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    int read()
    {
        int x = 0;
        int k = 1;
        char c = getchar();
        
        while (c > '9' || c < '0') 
            if (c == '-') c = getchar(), k = -1;
            else c = getchar();
        while (c >= '0' && c <= '9')
            x = (x << 1) + (x << 3) + (c ^ 48),
            c = getchar();
            
        return k * x;
    }
    
    short n, m;
    short z[70];
    short v[70];
    short c[70];
    short val[70][70];
    int cos[70][70];
    short cnt[70];
    int f[33000];
    
    int main()
    {
        n = read();
        m = read();
        for (int i = 1; i <= m; ++i)
            c[i] = read(), v[i] = read(), z[i] = read();
        
        int top = 0;
        short son[2], wic = 1; 
        for (int i = 1; i <= m; ++i)
            if (z[i] == 0)
            {
                ++top;
                val[top][++cnt[top]] = v[i] * c[i];
                cos[top][cnt[top]] = c[i];
                for (int j = 1; j <= m; ++j)
                    if (z[j] == i)
                    {
                        ++cnt[top];
                        val[top][cnt[top]] = v[i] * c[i] + c[j] * v[j];
                        cos[top][cnt[top]] = c[i] + c[j];
                        son[wic ^ 1] = j;
                        wic ^= 1;
                    }
                if (cnt[top] == 3)
                {
                    ++cnt[top];
                    val[top][cnt[top]] = v[i] * c[i] + c[son[0]] * v[son[0]] + c[son[1]] * v[son[1]];
                    cos[top][cnt[top]] = c[i] + c[son[0]] + c[son[1]];
                }
             }
        for (int i = 1; i <= top; ++i)
          for (int j = n; j >= 0; --j)
            for (int k = 1; k <= cnt[i]; ++k)
               if (j >= cos[i][k])
                  f[j] = max(f[j], f[j - cos[i][k]] + val[i][k]);
        cout << f[n];
    }
  • 相关阅读:
    nodejs利用windows API读取文件属性(dll)
    nodejs调用delphi编写的dll
    Ubuntu 安装配置Jenkins
    electron将网站打包成桌面应用
    NW.js将网站打包成桌面应用
    Jenkins配置邮件SMTP(使用QQ邮箱)
    Jenkins搭建Nodejs自动化测试
    Ubuntu 安装node.js
    使用superobject 解析Json数据
    使用superobject 新建Json数据(数组)
  • 原文地址:https://www.cnblogs.com/yanyiming10243247/p/9717580.html
Copyright © 2011-2022 走看看