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 comment block
12   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
13   * @authr songt
14   */
15 const int inf = 500000005;
16 const int imax_n = 10005;
17 int n;
18 int weight[505],value[505];
19 int dp[imax_n];
20 int v;
21 int e,f;
22 int min(int a,int b)
23 {
24     return a<b?a:b;
25 }
26 void Dp()
27 {
28     for (int i=0;i<=v;i++) dp[i]=inf;
29     dp[0]=0;
30     for (int i=1;i<=n;i++)
31     {
32         for (int j=weight[i];j<=v;j++)
33         {
34             dp[j]=min(dp[j],dp[j-weight[i]]+value[i]);
35         }
36     }
37     if (dp[v]==inf)
38     {
39         printf("This is impossible.\n");
40     }
41     else
42     {
43         printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]);
44     }
45 }
46 int main()
47 {
48     int T;
49     scanf("%d",&T);
50     while (T--)
51     {
52         scanf("%d%d",&e,&f);
53         v=f-e;
54         scanf("%d",&n);
55         for (int i=1;i<=n;i++)
56         scanf("%d%d",&value[i],&weight[i]);
57         Dp();
58     }
59     return 0;
60 }

时间: 2024-10-05 23:58:19

hdu1114 完全背包的相关文章

HDU1114(完全背包装满问题)

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

动态规划

int dp[1005][1005] = {0},len1,len2; char a[1005],b[1005]; void lcs() { for(int i = 1; i <= len1;i++) { for(int j = 1;j <= len2;j++) { if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1]+1; else dp[i][j] = max(dp[i-1][j],dp[i][j-1]); } } } void printflcs(in

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(完全背包)

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

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

hdu1114(完全背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 分析:很裸的一道完全背包题,只是这里求装满背包后使得价值最少,只需初始化数组dp为inf:dp[0]=0; 然后直接套入完全背包循环就行了... #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #incl

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