标签:
题意:给一个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 <algorithm> #include <cstdlib> #include <ctime> using namespace std; int p[555], w[555]; int dp[10003]; const int oo = 1e9; int main() { int t, e, f, n; scanf("%d", &t); while(t--) { scanf("%d%d", &e, &f); scanf("%d", &n); dp[0] = 0; for(int i=1; i<=n; i++) { scanf("%d%d", &p[i], &w[i]); } for(int i=1; i<=f-e; i++) dp[i]=oo; for(int i=1; i<=n; i++) { for(int j=w[i]; j<=f-e; j++) { dp[j] = min(dp[j], dp[j-w[i]]+p[i]); } } if(dp[f-e]!=oo) printf("The minimum amount of money in the piggy-bank is %d.\n", dp[f-e]); else printf("This is impossible.\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/mengzhong/p/5357510.html