标签:
其实这道题在寒假自学训练赛的时候就做过,不过感觉现在的理解跟当时真的不一样,大一半年过去了,变了这么多,我们都在进步,一回头便走出了这么远...
好了,题目很简单,一个背包,装不满做一个标记就行了
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <map> #include <algorithm> using namespace std; long long dp[10010]; int money[10010], weight[10010]; int main() { int t; scanf("%d",&t); while(t--) { int w1,w2; scanf("%d%d",&w1,&w2); int all = w2 - w1; dp[0] = 0; for(int i = 1;i <= all;i++) dp[i] = 999999999; int kind; scanf("%d",&kind); for(int i = 0;i < kind;i++) { scanf("%d%d",&money[i],&weight[i]); } for(int i = 0;i < kind;i++) { for(int j = weight[i]; j <= all;j++) { dp[j] = min(dp[j],dp[j-weight[i]] + money[i]); } } if(dp[all] == 999999999) printf("This is impossible.\n"); else printf("The minimum amount of money in the piggy-bank is %lld.\n",dp[all]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jifahu/p/5449254.html