PACM TEAM(小剪枝)

链接: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

感受:开始以为要用dp。后来实验室里其他人说暴力能过。于是就开始敲。然后发现自己一个地方写挫了。

代码中注释的地方就是我错误的地方。当时居然没有看出来~

还要有个小剪枝。用sum[i]记录在包括i和i后边的所有的和。然后判断一下如果后边都取,和ans进行比较大小。比ans小的话,直接return就好了。

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
int A,B,C,D;
int ans;
struct node
{
    int a,b,c,d,val;
    int id;
}lala[40];
int vis[40],viss[40];
int sum[40];

void dfs(int aa,int bb,int cc,int dd,int id,int money)
{
    ///cout<<"id:"<<id<<endl;
    if(id==n)
    {
       /// cout<<"ans"<<ans<<endl;
        if(ans<money)
        {
            ans=max(ans,money);
            for(int i=0;i<=n-1;i++)
            {
                if(vis[i]==1)
                    viss[i]=1;
                else
                    viss[i]=0;
            }
        }
      ///  cout<<"^^^"<<endl;
        return ;
    }
    if(money+sum[id]<ans)
    {
       /// cout<<"ppp"<<endl;
        return ;
    }
    vis[id]=1;

    int a1=lala[id].a;
    int b1=lala[id].b;
    int c1=lala[id].c;
    int d1=lala[id].d;

    /*
    if(aa+a1>A||bb+b1>B || cc+c1>C ||dd+d1>D)
    {
        if(ans<money)
        {
            ans=max(ans,money);
            for(int i=0;i<=n-1;i++)
            {
                if(vis[i]==1)
                    viss[i]=1;
                else
                    viss[i]=0;
            }
        }
        cout<<"***"<<endl;
        return;
    }
    */
    if(aa+a1<=A && bb+b1<=B && cc+c1<=C &&dd+d1<=D)
        dfs(aa+lala[id].a,bb+lala[id].b,cc+lala[id].c,dd+lala[id].d,id+1,money+lala[id].val);
    vis[id]=0;
    dfs(aa,bb,cc,dd,id+1,money);

}
int main()
{
    memset(vis,0,sizeof(vis));
    memset(viss,0,sizeof(viss));
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d%d%d",&lala[i].a,&lala[i].b,&lala[i].c,&lala[i].d,&lala[i].val);
        lala[i].id=i-1;
    }

    sum[n-1]=lala[n-1].val;

    for(int i=n-2;i>=0;i--)
    {
        sum[i]=lala[i].val+sum[i+1];///就是说选了这个之后所获得的最大价值(包含这个数)
    }
    scanf("%d%d%d%d",&A,&B,&C,&D);

    ans=0;
    dfs(0,0,0,0,0,0);
    int kaka=0;
    for(int i=0;i<=35;i++)
    {
        if(viss[i]==1)
            kaka++;
    }
    printf("%d\n",kaka);
    for(int i=0;i<=35;i++)
    {
        if(viss[i]==1)
           printf("%d\n",i);
    }

   /// cout<<ans<<endl;

}
/*
2
1 0 2 1 10
1 0 2 1 21
1 0 2 1

10
0 0 0 0 2
0 0 0 0 3
0 0 0 0 4
0 0 0 0 5
0 0 0 0 6
0 0 0 0 7
0 0 0 0 8
0 0 0 0 9
0 0 0 0 10
0 0 0 0 12
0 0 0 0


3
0 0 0 0 4
9 9 9 9 9
8 8 8 8 8
0 0 0 0



3
9 9 9 9 9
8 8 8 8 8
0 0 0 0 10
0 0 0 0

*/

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/81223496
今日推荐