求助!我也不知道我代码啥毛病,瞎敲的……

P1064 [NOIP2006 提高组] 金明的预算方案

Lee666666 @ 2020-12-23 21:22:33

#include <bits/stdc++.h>
using namespace std;
const int maxn = 60, maxm = 32000;
struct node {
    int w, c, q;
};
int dp[maxm + 15], f[maxm + 15], id[maxn + 15], sum[maxn + 15];
node p[maxn + 15];
vector<node> a[maxn + 15];
int n, m, len;

int max(int a, int b) {
    return a > b ? a : b;
}

int main(void) {
    std::ios::sync_with_stdio(0);
    cin >> m >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i].c >> p[i].w >> p[i].q;
        p[i].w *= p[i].c;
        if (p[i].q == 0) {
            id[i] = ++len;
            a[len].push_back(p[i]);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (p[i].q) {
            id[i] = id[p[i].q];
            a[id[i]].push_back(p[i]);
        }
    }
    for (int t = 1; t <= len; t++) {
        for (int i = 1; i < a[t].size(); i++) {
            sum[t] += a[t][i].c;
        }
    }
    for (int t = 1; t <= len; t++) {
        for (int i = 1; i < a[t].size(); i++) {
            for (int j = sum[t]; j >= 0; j--) {
                f[j] = max(f[j], f[j - a[t][i].c] + a[t][i].w);
            }
        }
        for (int i = sum[t]; i >= 0; i--) {
            f[i + a[t][0].c] = f[i] + a[t][0].w;
        }
        for (int i = 0; i < a[t][0].c; i++) {
            f[i] = 0;
        }
        for (int i = 0; i <= sum[t] + a[t][0].c; i++) {
            for (int j = m; j >= 0; j--) {
                dp[j] = max(dp[j], dp[j - i] + f[i]);
            }
        }
        for (int i = 0; i <= m; i++) {
            cout << dp[i] << " ";
        }
        cout << endl;
        memset(f, 0, sizeof(f));
    }
    cout << dp[m];
    return 0;
}

应该没人看得懂


|