牛客国庆day8 PACM Team(多重背包输出方案)

链接:https://ac.nowcoder.com/acm/contest/7865/D
来源:牛客网

题目描述
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.

Since 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).

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.

1 ≤ 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).

You 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
输出
复制
0

题意:
多重背包输出方案。

思路:
模板题,不解释了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

short int dp[37][37][37][37][37];

struct Node {
    
    
    int a,b,c,d,v;
}a[37];

int main() {
    
    
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
    
    
        scanf("%d%d%d%d%d",&a[i].a,&a[i].b,&a[i].c,&a[i].d,&a[i].v);
    }
    int A,B,C,D;scanf("%d%d%d%d",&A,&B,&C,&D);
    for(int i = 1;i <= n;i++) {
    
    
        for(int j = 0;j <= A;j++) {
    
    
            for(int k = 0;k <= B;k++) {
    
    
                for(int ci = 0;ci <= C;ci++) {
    
    
                    for(int cj = 0;cj <= D;cj++) {
    
    
                        dp[i][j][k][ci][cj] = dp[i - 1][j][k][ci][cj];
                        if(j < a[i].a || k <= a[i].b || ci <= a[i].c || cj <= a[i].d) continue;
                        dp[i][j][k][ci][cj] = (short )max((int)dp[i][j][k][ci][cj],(int)dp[i - 1][j - a[i].a][k - a[i].b][ci - a[i].c][cj - a[i].d] + a[i].v);
                    }
                }
            }
        }
    }
    
    vector<int>ans;
    for(int i = n;i >= 1;i--) {
    
    
        if(A < a[i].a || B < a[i].b || C < a[i].c || D < a[i].d) continue;
        if(dp[i][A][B][C][D] == dp[i - 1][A - a[i].a][B - a[i].b][C - a[i].c][D - a[i].d] + a[i].v) {
    
    
            ans.push_back(i);
            A -= a[i].a;
            B -= a[i].b;
            C -= a[i].c;
            D -= a[i].d;
        }
    }
    printf("%d\n",ans.size());
    for(int i = 0;i < ans.size();i++) {
    
    
        printf("%d ",ans[i] - 1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/109002187