NEU 1639: Small problem

来源:互联网 发布:vs2010读取excel数据 编辑:程序博客网 时间:2024/06/06 21:42

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.

样例输入

样例输出

提示

来源

暑期个人2



题目链接: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
原创粉丝点击