首页 > 代码库 > ACdream原创群赛(13)のwuyiqi退役专场 C True love

ACdream原创群赛(13)のwuyiqi退役专场 C True love

True love

Time Limit: 4000/2000 MS (Java/Others)     Memory Limit:128000/64000 KB (Java/Others)

Problem Description

Is there true love in the world?maybe or not, god knows! We know there are some complex relationships between ds and cxlove, they fell into love when they were rookies in acm,.

Now It‘s the season of graduation, it‘s also a season for lovers to say good-bye.But, ds and cxlove don‘t believe this. They want to show their true love and they plan to go out for a trip. But you know ds is a chihuo, he has a lot of snacks, now he wants to know how many different volumes he can take with a bag of a certain capacity.

Input

First line there is a t. represent the test cases.

Each test case begins with two integers n, cap 
(1 <= n <= 100, 0 <= cap <= 100000).
the next line contains n integers denoting the volume of the snacks.
a[1], a[2], a[3]...a[n];
1 <= a[i] <= 100000
the last line contains n integers denoting the number of the corresponding snack.
b[1], b[2], b[3]...b[n];
1 <= b[i] <= 1000

Output

please look at the Sample Output

Sample Input

2
2 10
1 2
1 1

2 2
1 2
1 1

Sample Output

Case 1: 3
Case 2: 2

n*m背包。貌似第一次做这样的背包题。。。

。,思想还是非常easy懂的。



AC代码例如以下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n,t,cap;
    int i,j;
    int a[105],b[105];
    int dp[100005],times[100005];
    int sum,cont=0;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof dp);
        sum=0;cont++;
        scanf("%d%d",&n,&cap);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&b[i]);
        for(i=1,dp[0]=1;i<=n;i++)
        {
            memset(times,0,sizeof times);
            for(j=a[i];j<=cap;j++)
            {
                if(!dp[j]&&dp[j-a[i]]&&times[j-a[i]]<b[i])//枚举添加a[i]能在已有基础上到达的数值。
                {
                    sum++;
                    times[j]=times[j-a[i]]+1;
                    dp[j]=1;
                }
            }
        }
        printf("Case %d: %d\n",cont,sum);
    }

    return 0;
}




ACdream原创群赛(13)のwuyiqi退役专场 C True love