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
- NEU 1639: Small problem
- small problem
- The Small Files Problem
- The Small Files Problem
- How to solve (Buffer too Small".0) problem?
- NEU 1052
- Google Code Jam 2015 Qualification Round: Problem D. Ominous Omino 【Small Input】
- NEU:F-大侠住店
- NEU 1253 线段树
- NEU 1212 VIJOS-P1037
- neu之路
- neu暑假集训
- NEU-1452: 滑雪大冒险
- Neu C详解
- NEU 1642: Laptop Battery
- NEU 1252 (拆位)
- NEU 1576 (DP)
- NEU 1008 Friends number
- 静下心来谈谈平衡二叉树
- python中单继承和多继承中子类默认继承父类的哪个构造函数__init__
- [Leetcode]Add Binary
- SQL Server 2008下日志清理方法
- ButterKnife官方介绍
- NEU 1639: Small problem
- [笔记][Java7并发编程实战手册]2.5使用Lock实现同步二
- Codeforces Round #315 (Div. 2) B. Inventory (水题)
- hdu 3501
- Blog转移(暂时/永久)
- Java反射浅谈
- 山科大牛总结
- iOS开发之OC实例可见度,方法
- 在freemarker的模板文件中调用自定义的方法