HDOJ-1114(完全背包模板题)
Piggy-Bank
HDOJ-1114
本题就是完全背包的模板题,注意复习一下关于背包九讲中的问什么这里使用的是顺序遍历。
还需要注意的一个问题就是初始化的问题,dp[0]初始化为0,其他的初始化为无穷大。因为最后的状态是背包一定是满的。(具体看背包九讲的ppt的解释)
//完全背包的问题
#include<bits/stdc++.h>//使用G++提交
// #include<iostream>
// #include<algorithm>
// #include<cstring>
// #include<cstdio>
using namespace std;
const int INF=0X3F3F3F3F;
const int maxw=10005;
int empty,full;
int n;
int v[505],w[505];//value and weight
int dp[maxw];//dp[i][j]表示选择前i种物品放入重量为j的存钱罐中最少的钱
int main(){
int t;
cin>>t;
while(t--){
cin>>empty>>full;
int u=full-empty;//存钱罐中钱的重量
cin>>n;
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++){
cin>>v[i]>>w[i];
}
for(int i=1;i<=n;i++){
for(int j=w[i];j<=u;j++){
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[u]==INF){
cout<<"This is impossible."<<endl;
}else
cout<<"The minimum amount of money in the piggy-bank is "<<dp[u]<<"."<<endl;
}
//system("pause");
return 0;
}
HDOJ-1114(完全背包模板题)的更多相关文章
- HDU 1114 Piggy-Bank(完全背包模板题)
完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- 51nod 1086背包问题V2 (完全背包模板题)
1086 背包问题 V2 1 秒 131,072 KB 20 分 3 级题 题目描述 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1, ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
- 洛谷P2918 [USACO08NOV]买干草(一道完全背包模板题)
题目链接 很明显的一道完全背包板子题,做法也很简单,就是要注意 这里你可以买比所需多的干草,只要达到数量就行了 状态转移方程:dp[j]=min(dp[j],dp[j-m[i]]+c[i]) 代码如下 ...
- 分组背包模板题 hdu1712
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...
- 01背包模板题 hdu2602 Bonecollector
由于数组的滚动过程中当前值(i,j)的更新需要用到上一层的(i-1,j-wi)的值,所以在更新当前的j之前不能更新上一层的j之前的值,故01背包是从后向前更新的(重量取值是从大到小的). 代码如下: ...
- ACboy needs your help-分组背包模板题
id=17676" target="_blank" style="color:blue; text-decoration:none">ACboy ...
随机推荐
- Atcoder ABC161 A~E
传送门 A - ABC Swap 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 ...
- 一维二维Sparse Table
写在前面: 记录了个人的学习过程,同时方便复习 Sparse Table 有些情况,需要反复读取某个指定范围内的值而不需要修改 逐个判断区间内的每个值显然太浪费时间 我们希望用空间换取时间 ST表就是 ...
- 新疆大学ACM新生赛(公开赛) E.异或 (思维,位运算)
题意:RT 题解: \(i\ mod \ k=0\),即所有事\(k\)的倍数的位置都要进行异或,根据异或的性质,我们知道如果相同的异或的数个数是偶数的话,得出的结果是\(0\),所以每次询问,我们判 ...
- C - C工程编译那些事【configure-make || cmake-make】
一.cofigure是怎么生成的,我们又是怎么使用的 configure和make install背后的故事: https://azyet.github.io/2015/06/20/configure ...
- 4.Direct交换机之使用指定routingkey完成日志记录场景
标题 : 4.Direct交换机之使用指定routingkey完成日志记录场景 目录 : RabbitMQ 序号 : 4 const string logOthersQueueName = " ...
- vs2019 写入访问权限冲突
先说句题外话 vs反应有时候有点慢,改过的地方等几秒才会显示正确 另外有时候正确的地方会报错,重启吧 回到正题 "引发了异常: 写入访问权限冲突._Left 是 0xCDCDCDCD.如有适 ...
- hdu-1941 Find the Shortest Common Superstring
The shortest common superstring of 2 strings S 1 and S 2 is a string S with the minimum number of ch ...
- git commit guidelines
git-commit-guidelines AngularJS Development Setup Running Tests Coding Rules Commit Message Guidelin ...
- web 存储方式汇总:Cookies,Session, Web SQL; Web Storage(LocalStorage ,SessionStorage),IndexedDB,Application Cache,Cache Storage
1 1 1 web 存储方式汇总: 旧的方式: Cookies; Session; Web SQL; 新的方式 HTML5 : Web Storage(LocalStorage ,SessionSto ...
- js console.log all in one
js console.log all in one this & arguments "use strict"; /** * * @author xgqfrms * @li ...