hdu1114

完全背包的水题,不过今天才学动态规划,就这样啦……hahahah!!!

完全背包跟普通背包的区别是普通背包从后往前循环,以防止被替换

完全背包是从前往后循环,后面的状态会跟着之前状态的改变而改变……

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#define Maxn 0xfffffff

using namespace std;

int cmp(int x,int y){
    return x < y?x:y;
}

//int dp[1100][1100];
int v[11000];
int w[11000];
int dp[11000];

int main()
{
  //  freopen("input.txt","r",stdin);
    int t;
    cin >> t;
    while(t--){
        int M;
        int V,n;
        cin >> M >> V;
        V -= M;
        cin >> n;
        for(int i = 1;i <= n;i++){
            cin >> v[i] >> w[i];
        }
        for(int i = 0;i <= V;i++)
            dp[i] = Maxn;
        dp[0] = 0;
        for(int i = 1;i <= n;i++){
            for(int j = w[i];j <= V;j++){
                dp[j] = cmp(dp[j],dp[j-w[i]]+v[i]);
            }
        }
        if(dp[V] == Maxn)
            cout << "This is impossible." << endl;
        else
            cout << "The minimum amount of money in the piggy-bank is " <<dp[V] <<"."<< endl;
    }
    return 0;
}

时间: 2024-07-30 19:15:22

hdu1114的相关文章

hdu1114 Piggy-Bank 完全背包

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes

hdu1114 Piggy-Bank (DP基础 完全背包)

链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量j的最小花费 g[j]=min(g[j],g[j-w[i]]+v[i]) 完全背包物品可以多次使用,所以j的循环要正着来. 代码: 1 #include<cstdio> 2 #include<cmath> 3 #include<iostream> 4 #include<

hdu1114 Piggy-Bank

题意:给一个t,表示t组样例,接下来输入e,f 表示罐子重e,装满后重f,输入n,接下来n行,有n中钱,分别有w[i],p[i],两个属性,问,罐子装满后最少的钱,如果不能装满输出This is impossible. 分析:明显的完全背包问题(一开始写超时了,转化成了01背包写,没想到也超时了,后来发现dp数组开的太小了) #include <iostream> #include <cstdio> #include <cstring> #include <alg

HDU-1114(背包DP)

Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM

HDU1114 Piggy-Bank(完全背包)

题意:给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量.然后给了n种硬币的质量和价值. 问储钱罐里最少有多少钱. 解法:完全背包.注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible. /* *********************************************** Author :devil Created Time :2015/12/21 21:16:56 ***********************************

限制重量求最小价值的完全背包 HDU1114

1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 int v[510]; 7 int w[510]; 8 int dp[10010]; 9 10 int main() 11 { 12 int t; 13 cin>>t; 14 while(t--) 15 { 16 int a,b; 17 cin>>a>>b; 18 int n; 19 cin>>

hdu1114 完全背包

1 //Accepted 364 KB 109 ms 2 //多重背包 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a documentation

HDU1114 Piggy-Bank 【完全背包】

Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11149    Accepted Submission(s): 5632 Problem Description Before ACM can do anything, a budget must be prepared and the necessary fina

hdu1114 dp(完全背包)

题意:已知空钱罐质量和满钱罐质量(也就是知道钱罐里的钱的质量),知道若干种钱币每种的质量以及其价值,钱币都是无限个,问最少钱罐中有多少钱. 这个题在集训的时候学长给我们做过,所以你会做是应该的,由于已经有固定的质量,所以是必须正好放满的完全背包问题.然后```具体过程就不细讲了完全背包依旧是经典,你要是还不会就滚回去看背包九讲并且无颜见学长们了``` 1 #include<stdio.h> 2 #include<string.h> 3 #define min(a,b) a<b