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(完全背包模板题)的更多相关文章

  1. HDU 1114 Piggy-Bank(完全背包模板题)

    完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  2. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  3. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  4. 51nod 1086背包问题V2 (完全背包模板题)

    1086 背包问题 V2 1 秒 131,072 KB 20 分 3 级题 题目描述 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1, ...

  5. POJ 3624 Charm Bracelet(01背包模板题)

    题目链接 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52318   Accepted: 21912 Descriptio ...

  6. 洛谷P2918 [USACO08NOV]买干草(一道完全背包模板题)

    题目链接 很明显的一道完全背包板子题,做法也很简单,就是要注意 这里你可以买比所需多的干草,只要达到数量就行了 状态转移方程:dp[j]=min(dp[j],dp[j-m[i]]+c[i]) 代码如下 ...

  7. 分组背包模板题 hdu1712

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...

  8. 01背包模板题 hdu2602 Bonecollector

    由于数组的滚动过程中当前值(i,j)的更新需要用到上一层的(i-1,j-wi)的值,所以在更新当前的j之前不能更新上一层的j之前的值,故01背包是从后向前更新的(重量取值是从大到小的). 代码如下: ...

  9. ACboy needs your help-分组背包模板题

    id=17676" target="_blank" style="color:blue; text-decoration:none">ACboy ...

随机推荐

  1. 2020牛客暑期多校训练营(第二场) F.Fake Maxpooling (单调队列)

    题意:有一个\(n\)x\(m\)的矩阵,\(A_{i,j}=lcm(i,j)\),对于每个\(k\)x\(k\)的子矩阵,其最大元素贡献给答案,求答案的最大值. 题解:矩阵构成我们直接\(i*j/g ...

  2. .net core mvc 获取Web根目录和内容根目录的物理路径

    从ASP.NET Core RC2开始,可以通过注入 IHostingEnvironment 服务对象来取得Web根目录和内容根目录的物理路径,如下所示: using Microsoft.AspNet ...

  3. UML类图设计神器 AmaterasUML 的配置及使用

    最近写论文需要用到UML类图,但是自己画又太复杂,干脆找了个插件,是Eclipse的,也有IDEA的,在这里我简单说下Eclipse的插件AmaterasUML 的配置与使用吧. 点击这里下载Amat ...

  4. Doris开发手记1:解决蛋疼的MySQL 8.0连接问题

    笔者作为Apache Doris的开发者,平时感觉相关Doris的文章写的很少.主要是很多时候不知道应该去记录一些怎么样的问题,感觉写的不好就会很慌张.新的一年,希望记录自己在Doris开发过程之中所 ...

  5. VuePress 最新教程

    VuePress 最新教程 https://vuepress.vuejs.org/ https://github.com/vuejs/vuepress VuePress plugins 插件通常会为 ...

  6. CI / CD in Action

    CI / CD in Action Continuous Integration (CI) & Continuous Delivery (CD) https://github.com/mark ...

  7. modal 遮罩层,滚动穿透 bug

    modal 遮罩层,滚动 穿透bug float 弹层 taro 小程序弹框 滚动击穿 问题 https://segmentfault.com/q/1010000011134345 solution ...

  8. git & Angular git commit 规范

    git & Angular git commit 规范 https://github.com/angular/angular/commits/master https://github.com ...

  9. linux cd root directory

    linux cd root directory https://www.cyberciti.biz/faq/how-to-change-directory-in-linux-terminal/ htt ...

  10. ng 设置动态的document title

    使用Title服务 相关文章 配置路由, 添加data.title参数 import { NgModule } from '@angular/core'; import { RouterModule, ...