洛谷 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];
}

 

posted @ 2018-09-28 11:16  Christopher_Yan  阅读(164)  评论(0编辑  收藏  举报