求调,不行了

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

I_love_LPN_Forever @ 2022-09-03 13:59:58

50pts

#include <bits/stdc++.h>
using namespace std;
const int maxn = 32005;
int dp[maxn], V, n;
int v[maxn], p[maxn], f[maxn][2], x[maxn];
int main() {
    cin >> V >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i] >> p[i] >> x[i];
        if (x[i] != 0) {
            if (f[x[i]][0])
                f[x[i]][1] = i;
            else
                f[x[i]][0] = i;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = V; j >= 0; j--) {
            if (x[i]) continue;
            if (j >= v[i])
                dp[j] = max(dp[j], dp[j - v[i]] + (v[i] * p[i]));
            if (f[i][0]) {
                if (j >= v[i] + v[f[i][0]])
                    dp[j] = max(dp[j], dp[j - v[i] - v[f[i][0]]] + (v[i] * p[i]) + (v[f[i][0]] * p[f[i][0]]));
            }
            if (f[i][1]) {
                if (j >= v[i] + v[f[i][1]])
                    dp[j] = max(dp[j], dp[j - v[i] - v[f[i][1]]] + (v[i] * p[i]) + (v[f[i][1]] * p[f[i][1]]));
            }
            if (f[i][0] && f[i][1]) {
                if (j >= v[i] + v[f[i][1]] + f[i][0])
                    dp[j] = max(dp[j], dp[j - v[i] - v[f[i][1]] - v[f[i][0]]] + (v[i] * p[i]) + (v[f[i][1]] * p[f[i][1]]) + (v[f[i][0]] * p[f[i][0]]));
            }
        }
    }
    cout << dp[V] << endl;
    return 0;
} 

by _Winham_ @ 2022-09-03 14:26:09

你试一下把分支的if改为elif试一下


by JwJ_2010 @ 2022-09-03 14:45:59

@winham 好家伙你家C++用elif


by _Winham_ @ 2022-09-03 14:53:50

else if


by _Winham_ @ 2022-09-03 14:54:10

打错了


by _Winham_ @ 2022-09-03 14:57:57

#include <bits/stdc++.h>
using namespace std;
struct node{
    int zv,zp,fv1,fp1,fv2,fp2;
}; 

node a[100];
int maxn,n;
int dp[35000];

int main(){
    cin>>maxn>>n;
    int v,p,q;
    for(int i = 1;i <= n;i++){
        cin>>v>>p>>q;
        if(q == 0){
            a[i].zv = v;
            a[i].zp = v * p;
        }else{
            if(a[q].fp1 == 0){
                a[q].fv1 = v;
                a[q].fp1 = v * p;
            } else{
                a[q].fv2 = v;
                a[q].fp2 = v * p;
            }
        } 
    }

    for(int i = 1;i <= n;i++){
        if(a[i].zp == 0) continue; 

        for(int j = maxn;j >= a[i].zv;j--){
            dp[j] = max(dp[j],dp[j-a[i].zv] + a[i].zp);
            if(a[i].zv+a[i].fv1<=j) dp[j] = max(dp[j],dp[j-a[i].zv-a[i].fv1]+a[i].zp+a[i].fp1);
            if(a[i].zv+a[i].fv2<=j) dp[j] = max(dp[j],dp[j-a[i].zv-a[i].fv2]+a[i].zp+a[i].fp2);
            if(a[i].zv+a[i].fv1+a[i].fv2<=j) 
                dp[j] = max(dp[j],dp[j-a[i].zv-a[i].fv1-a[i].fv2]+a[i].zp+a[i].fp1+a[i].fp2);
        } 
    } 

    cout<<dp[maxn];
    return 0;
}

by _Winham_ @ 2022-09-03 14:58:38

你方法有点麻烦,试一下我的


|