牛客2018暑假多校第三场A PACM Team(01背包+纪录路径)

题目链接:https://www.nowcoder.com/acm/contest/141/A

链接:https://www.nowcoder.com/acm/contest/141/A
来源:牛客网
 

题目描述

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

四维的01背包+纪录路径 本来没啥的  但是这题内存限制比较过分  开五维int会炸 没办法按正常纪录路径

解决方法是将int缩小为bool  然后表明到达这个状态的最大值有没有被第五维上的物品影响

如果向上发现没有 说明不是被他纪录 再往上一层 有点暴力的想法在里面

#include<bits/stdc++.h>
using namespace std;
struct xjy
{
    int a,b,c,d,val;
};
int dp[40][40][40][40];
bool pre[40][40][40][40][40];
vector<xjy > s;
vector<int >ans;
int main()
{
    int A,B,C,D;
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        xjy mid;
        cin >> mid.a >> mid.b>> mid.c >> mid.d >> mid.val;
        s.push_back(mid);
    }
    cin >> A >> B >>C >>D;
    for(int i=0;i<n;i++)
    {
        for(int j=A;j>=s[i].a;j--)
        {
            for(int k=B;k>=s[i].b;k--)
            {
                for(int l=C;l>=s[i].c;l--)
                {
                    for(int t=D;t>=s[i].d;t--)
                    {
                        if(dp[j][k][l][t]<dp[j-s[i].a][k-s[i].b][l-s[i].c][t-s[i].d]+s[i].val)
                        {
                            dp[j][k][l][t]=dp[j-s[i].a][k-s[i].b][l-s[i].c][t-s[i].d]+s[i].val;
                            pre[i+1][j][k][l][t]=1;
                        }
                    }
                }
            }
        }
    }
    int nowi=A;
    int nowj=B;
    int nowk=C;
    int nowl=D;
    int mid=n;
    while(mid>=1&&dp[nowi][nowj][nowk][nowl]>0)
    {
        if(pre[mid][nowi][nowj][nowk][nowl])
        {
            ans.push_back(mid-1);
            nowi-=s[mid-1].a;
            nowj-=s[mid-1].b;
            nowk-=s[mid-1].c;
            nowl-=s[mid-1].d;
        }
        mid--;
    }
    int len=ans.size();
    cout << len << endl;
    for(int i=0;i<len;i++)
    {
        cout << ans[i] << " ";
    }
}

猜你喜欢

转载自blog.csdn.net/xuejye/article/details/81227456