HDU1114
题意
给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量,然后给了n种硬币的质量和价值,问储钱罐里最少有多少钱
分析
完全背包
注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible.
初始化dp[0] = 0,其余都为INF,因为只有开始时0转移过来的才是合法的
View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1e5+10; int dp[maxn]; int st,ed; int n; int c[maxn], a[maxn]; int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &st, &ed); int v = ed - st; scanf("%d",&n); for(int i = 1; i <= n; i++) { scanf("%d%d", &c[i], &a[i]); } for(int i = 1;i <= 1e5; i++) dp[i]=1e9; // dp[v]=1e9; dp[0]=0; for(int i = 1; i <= n; i++) { for(int j = a[i]; j <= v; j++) { dp[j] = min(dp[j], dp[j-a[i]] + c[i]); } } if(dp[v]!=1e9) printf("The minimum amount of money in the piggy-bank is %d.\n", dp[v]); else printf("This is impossible.\n"); } return 0; }
要么优秀要么生锈