标签:
4
1 2 5
2 1 6
2 2 2
2 2 3
5 5
13
/* 二维费用01背包 */ #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; const int maxn = 105,inf = 9876543; int read(){ char ch=getchar(); int x=0,f=1; while(!(ch>=‘0‘&&ch<=‘9‘)){if(ch==‘-‘)f=-1;ch=getchar();}; while(ch>=‘0‘&&ch<=‘9‘){x=x*10+(ch-‘0‘);ch=getchar();}; return x*f; } int n,m,r; int rmb[maxn],rp[maxn],tm[maxn]; int dp[maxn][maxn],rec[maxn][maxn]; int ans1,ans2; int main(){ n = read(); for(int i = 1;i <= n;i++){ rmb[i] = read(); rp[i] = read(); tm[i] = read(); } m = read(); r = read(); for(int i = 0;i <= 100;i++){ for(int j = 0;j <= 100;j++){ rec[i][j] = inf; } } ans2 = inf; for(int i = 1;i <= n;i++){ for(int j = m;j >= rmb[i];j--){ for(int k = r;k >= rp[i];k--){ if(dp[j][k] < dp[j-rmb[i]][k-rp[i]] + 1){ dp[j][k] = dp[j-rmb[i]][k-rp[i]] + 1; if(rec[j-rmb[i]][k-rp[i]] >= inf) rec[j][k] = tm[i]; else rec[j][k] = rec[j-rmb[i]][k-rp[i]] + tm[i]; }else if(dp[j][k] == dp[j-rmb[i]][k-rp[i]] + 1){ if(rec[j-rmb[i]][k-rp[i]] >= inf) rec[j][k] = min(rec[j][k],tm[i]); else rec[j][k] = min(rec[j][k],rec[j-rmb[i]][k-rp[i]] + tm[i]); } if(ans1 < dp[j][k]){ ans1 = dp[j][k]; ans2 = rec[j][k]; }else if(ans1 == dp[j][k]){ ans2 = min(ans2,rec[j][k]); } } } } if(!ans1) cout<<0; else cout<<ans2; /*for(int i = 0;i <= m;i++){ for(int j = 0;j <= r;j++){ cout<<dp[i][j]<<" "; } cout<<endl; }*/ return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5924816.html