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;
}

原文地址:https://www.cnblogs.com/GarrettWale/p/11632046.html

时间: 2024-12-14 18:29:29

HDOJ-1114(完全背包模板题)的相关文章

1085 背包问题(0-1背包模板题)

1085 背包问题(0-1背包模板题)(51NOD基础题) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数,N和W中间用空格隔开.N为物品的数量,W为背包的容量.(1 <= N <= 100,1 <= W <= 10000) 第2 - N + 1行,每行

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &

hihocoder第七周 完全背包模板题

时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说之前的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 等等,这段故事为何似曾相识?这就要从平行宇宙理论说起了………总而言之,在另一个宇宙中,小Ho面临的问题发生了细微的变化! 小Ho现在手上有M张奖券,而奖品区有N种奖品,分别标号为1到N,其中第i种奖品需要need(i)张奖券进行兑换,并且可以兑换无数次,为了使得辛苦得到的奖券不白白浪费,小Ho给每件奖品都评了分,其

2019ACM-ICPC沈阳网络赛-C-Dawn-K&#39;s water(完全背包模板题)

Dawn-K's water  1000ms 262144K Dawn-K recently discovered a very magical phenomenon in the supermarket of Northeastern University: The large package is not necessarily more expensive than the small package. On this day, Dawn-K came to the supermarket

hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8052    Accepted Submission(s): 3291 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

Dividing-多重背包模板题

Dividing Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the

You Like Cake(超大01背包模板题)

You Like Cake 题目描述 双十一就要来啦!而Yuno刚刚获得了一笔X元的奖金.那么是不是应该清空下购物车呢? 购物车总共有N个物品,每个物品的价格为Vi,Yuno想尽可能地把手头的奖金给花光,所以她要精心挑选一些商品,使得其价格总和最接近但又不会超过奖金的金额.那么Yuno最后最少可以剩下多少钱呢? 输入 第一行,两个正整数N和X. 第二行,N个正整数Vi表示第i个物品的价格. 输出 输出一个整数,表示Yuno最后最少可以剩下的钱数. 样例输入 4 50 1 2 3 4 样例输出 4

hdu1712ACboy needs your help 分组背包模板题

//a[i][j] 表示花j天做第i件事得到的收获 //m天时间得到的最大收获 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 110 ; int dp[maxn] ; int a[maxn][maxn] ; int main() { int n , m ; while(scanf("%d%d" ,&n ,

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

ACboy needs your help Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different