hdu 4152 ZZY’s Dilemma【DFS】

ZZY’s Dilemma

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


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 i th integer represents the effect on i th 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

Author
ZZY@USC
 

Source
 

题目大意:

给你n个习惯的标准值,给你q个小习惯,问能保留的最多的习惯数和习惯数的标号。

思路:因为n,q都比较小,暴力枚举是不会TLE的,我们用vis【i】==1的时候表示这个习惯选择要,vis【i】==0的时候表示这个习惯不要,我们从第1个习惯开始枚举,当枚举到最后一个习惯的时候,我们判断这个习惯方法是不是最优的,如果是最优的,更新结果。

注意的点:小心PE,因为0的时候后边没有空格。

AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int b[25];
int a[25][25];
int vis[25];
int ans[25];
int anscont;
int q;
int n;
void dfs(int now,int sum[25])
{
    if(now==q)
    {
        int ok=1;
        int cont=0;
        for(int i=0;i<n;i++)
        {
            if(vis[i]==1)cont++;
            if(sum[i]<b[i])
            {
                ok=0;
                break;
            }
        }
        if(ok==0)return ;
        if(anscont<cont)
        {
            anscont=cont;
            cont=0;
            for(int i=0;i<q;i++)
            {
                if(vis[i]==1)
                {
                    ans[cont]=i+1;
                    cont++;
                }
            }
        }
        return ;
    }
    int tmp[25];
    for(int i=0;i<n;i++)
    {
        tmp[i]=sum[i];
    }
    int ok=1;
    for(int i=0;i<n;i++)
    {
        sum[i]+=a[now][i];
    }
    vis[now]=1;
    dfs(now+1,sum);
    vis[now]=0;
    dfs(now+1,tmp);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        int sum[25];
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        anscont=0;
        dfs(0,sum);
        if(anscont!=0)
        printf("%d ",anscont);
        else printf("0");
        int f=0;
        for(int i=0;i<anscont;i++)
        {
            if(f==0)
            printf("%d",ans[i]);
            else printf(" %d",ans[i]);
            f++;
        }
        printf("\n");
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值