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

本文通过一个具体案例介绍了一种背包问题的解决方法。该问题涉及计算不同体积零食在特定容量限制下所能组成的容量总数。文章提供了详细的AC代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}




转载于:https://www.cnblogs.com/jhcelue/p/7191762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值