二进制枚举

ZZY’s Dilemma


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 286    Accepted Submission(s): 117




Problem Description
ZZY has many habits like seeing movie, listening music, playing PC game and football and so on. Besides he has many goals, as not every habit is good for achieving his goals, he falls into the dilemma of making a choice between his goals and habits.
Now ,we define the effect that one habit has on each goal is represented as a vector,and the elements of the vector are integers,ex.vector(100,90,-10,80)means it has 100 point effect on goal 1,90 point effect on goal 2,-10 point effect on goal 3 and 80 point effect on goal 4(the positive point means good effect and the negative point means bad effect),and the given requirement of each goal is represented as integer.please help ZZY to achieve his goals as well as keeps his habits as many as possible. 
 


Input
There are multi cases , read the data until EOF.( No more than 10 cases ) 
Line 1: The number of ZZY’s goals N(0<N<=20) 
Line 2: The requirement of each goals (0 < w <= 1000)
Line 3: The number of ZZY’s habits M(0 < M <= 16)
Line 4-M+4: Each line contains N integers, the ith integer represents the effect on ith goal (-1000 <= data <= 1000).
 


Output
For each case: The output is a single line that contains:
* the maximum number of habits ZZY can keep, followed by:
* a SORTED list (from smallest to largest) of the habit ZZY can keep. If more than one set of habits could meet the requirement, choose the set with the smallest habit numbers.
Just please output 0 if there is no way to achieve ZZY’s goals.
 


Sample Input
4
100 200 300 400
3
100 100 400 500
100 -10 50  300
100 100 -50  -50
 


Sample Output
2 1 3
题意: ZZY 有 N (N<=20) 个目标,每个目标都有个只是达到的分数值, 然后他有 M (M<=16)
个习惯,每个习惯对这 N 个目标有影响 xi (i>=1 ,i<=N),xi<0表示对目标 i 的实现坏的影
响,否则其积极左右。现在问你如果 ZZY 想实现 N 个目标,他说能够有的习惯的数目最大
是多少,并输出;
思路: 总共有 M 个习惯, 如果用0 1 两种状态表明选还是不选, 那么共有 2^M 中情况 (2^16
才65536) ,时间复查度(2^M *M)
Accepted 4152 93MS 480K 1269 B
我的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int habit[20][25];
int goal[25];
int gg[25],n,m,ans1,ans2;
void dfs()
{
    int i,j,k,a,b,f,ans,num,oo;
    ans1=-1;
    for(i=1;i<=1<<m;i++)
    {
        k=i;
        a=0;
        num=0;
        memset(gg,0,sizeof(gg));
        while(k>0)
        {
          if(k%2)
          {
              num++;
              for(j=0;j<n;j++)
              gg[j]+=habit[a][j];
          }
          k/=2;
          a++;
        }
        f=0;
        for(j=0;j<n;j++)
        {
            if(gg[j]<goal[j])
            f=1;
        }
        if(f==0)
        {
        if(num>ans1){ans1=num;ans2=i;}
        else  if(num==ans1&&ans2>i){ans1=num;ans2=i; }
        }


    }


}
int main()
{
   int i,j,k;
   while(scanf("%d",&n)!=EOF)
   {
       for(i=0;i<n;i++)
       scanf("%d",&goal[i]);
       scanf("%d",&m);
       for(i=0;i<m;i++)
       for(j=0;j<n;j++)
       {
           scanf("%d",&habit[i][j]);
       }
       dfs();
       if(ans1==-1)puts("0");
       else
       {
        printf("%d",ans1);
       for(i=0;i<m;i++)
       if(ans2&(1<<i))
       printf(" %d",i+1);
       printf("\n");
       }
   }
   return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值