poj1384完全背包恰好装满

来源:互联网 发布:苹果数据漫游要打开吗 编辑:程序博客网 时间:2024/04/20 07:32
#include <cstdio>#include <iostream>#include <algorithm>#define N 10050using namespace std;const int inf = 0x3f3f3f3f;int n, v;//物品的种数,背包的容量int dp[N], c[N], w[N];void init()//恰好装满的初始化{for (int i = 1; i <= v; i++)dp[i] = inf;dp[0] = 0;}int main(){freopen("e:\\input.txt", "r", stdin);int T;scanf("%d", &T);while (T--){int full, empty;scanf("%d%d", &empty, &full);scanf("%d", &n);v = full - empty;init();for (int i = 1; i <= n; i++)scanf("%d%d", &w[i], &c[i]);for (int i = 1; i <= n; i++)for (int j = c[i]; j <= v; j++)dp[j] = min(dp[j], dp[j - c[i]] + w[i]);//表示放与不放if (dp[v] == inf)printf("This is impossible.\n");elseprintf("The minimum amount of money in the piggy-bank is %d.\n", dp[v]);}return 0;}

0 0