NEU 1639: Small problem

1639: Small problem

时间限制:  1 Sec   内存限制:  128 MB

题目描述

 This is a small question.Here is a bag, at first the bag weights n, after shopping, the bag.s weight become m(1<=n,m<=100000).Now I will give all the k products in the market(1<=k<=500).Can you tell me the minimum cost if I need to fill the bag.If can't do it, please print out "NO" to tell me.(1<=weight,cost<=100000)

输入

 Here are nearly 100 cases, T first

then there is n and m

the 3rd line is the kind of the product k

then k lines show the each product's weight and cost

输出

 as is described above.

样例输入

样例输出

提示

来源



题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1639

分析:完全背包问题,直接套模板。


模板1:(01背包)

for(int i=0;i<n;i++){
	for(int j=W;j>=w[i];j--){
        dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
    }
}
cout<<dp[W]<<endl;


模板2:(完全背包)

for(int i=0;i<n;i++){
	for(int j=w[i];j<=W;j++){
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
        }
}
cout<<dp[W]<<endl;


CODE:

#include <iostream>
#include <string.h>
#include <cmath>
using namespace std;

int main()
{
    const int maxn=1e5+5;
    int t,n,m,k,w[505],c[505];
    int dp[100005];
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        for(int i=0;i<k;i++)
            cin>>w[i]>>c[i];
        for(int i=0;i<100005;i++)
            dp[i]=maxn;
        dp[0]=0;
        for(int i=0;i<k;i++){
            for(int j=w[i];j<=m-n;j++){
                dp[j]=min(dp[j],dp[j-w[i]]+c[i]);
            }
        }
        if(dp[m-n]==maxn)
            cout<<"NO"<<endl;
        else
            cout<<dp[m-n]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值