1. 程式人生 > >ACdream原創群賽(13)のwuyiqi退役專場 C True love

ACdream原創群賽(13)のwuyiqi退役專場 C True love

ems namespace ade scan nsh user space please code

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