連接:https://www.nowcoder.com/acm/contest/141/A
來源:牛客網數組時間限制:C/C++ 1秒,其餘語言2秒
空間限制:C/C++ 262144K,其餘語言524288K
Special Judge, 64bit IO Format: %lld
題目描述
Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(implementation ability), then math. However, in the ACM ICPC World Finals 2018, Eddy failed to solve a physics equation, which pushed him away from a potential medal.appSince then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math). ide
There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy's magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn't want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.優化
Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn't exceed the constraint and will bring the most knowledge points in total.
輸入描述:
The first line contains a positive integer N indicating the number of candidate groups.
Each of following N lines contains five space-separated integer pi, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points.
The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.ui1 ≤ N ≤ 36
0 ≤ pi,ai,ci,mi,gi ≤ 36
0 ≤ P, A, C, M ≤ 36
輸出描述:
The first line should contain a non-negative integer K indicating the number of invited groups.
The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).spaYou can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.
示例1
輸入
2
1 0 2 1 10
1 0 2 1 21
1 0 2 1
輸出
1
1
示例2
輸入
1
2 1 1 0 31
1 0 2 1
輸出
0code
多維費用的揹包問題,對於每件物品,具備pi,ai,ci,mi四種費用,具備gi的價值。選擇這件物品必須同時付出四種費用。問怎麼選擇物品能夠獲得最大的價值。orm
狀態轉移方程:F[i,p,a,c,m] = max( F[i-1][p][a][c][m], F[i-1][p-Pi] [a-Ai][c-Ci][m-Mi] + Gi )blog
由於5維的數組可能超內存,因此能夠採用逆序的循環優化掉第一維度(滾動數組)ip
在轉移的過程要用一個bool型的數組記錄一下,以便後面回溯找出揹包加入了哪些物品
#include<cstdio> #include<algorithm> #include<vector> using namespace std; vector<int> ans; int maxn = 100; int p[40],a[40],c[40],m[40],g[40]; int P,A,C,M; int dp[40][40][40][40]; bool path[37][37][37][37][37]; int main() { int k; scanf("%d",&k); for(int i=1;i<=k;i++){ scanf("%d%d%d%d%d",&p[i],&a[i],&c[i],&m[i],&g[i]); } scanf("%d%d%d%d",&P,&A,&C,&M); for(int i=1;i<=k;i++){ for(int pp=P;pp>=p[i];pp--){ for(int aa=A;aa>=a[i];aa--){ for(int cc=C;cc>=c[i];cc--){ for(int mm=M;mm>=m[i];mm--){ int add = dp[pp-p[i]][aa-a[i]][cc-c[i]][mm-m[i]]+g[i]; if(add>dp[pp][aa][cc][mm]){ dp[pp][aa][cc][mm]=add; path[i][pp][aa][cc][mm]=1;///記錄這裏轉移 } } } } } } ///回溯加入的物品 int pp = P,aa=A,cc=C,mm=M; for(int i=k;i>=1;i--){ if(path[i][pp][aa][cc][mm]){ ans.push_back(i-1); ///題目裏物品的下標從0開始,可是代碼裏從1開始,因此減去1 pp-=p[i]; aa-=a[i]; cc-=c[i]; mm-=m[i]; } } printf("%d\n",ans.size()); for(int i=0;i<ans.size();i++){ if(i==ans.size()-1) printf("%d\n",ans[i]); else printf("%d ",ans[i]); } return 0; }