Question (poj) 1170:
There are many commodities, and no commodity has a unit price. You can buy a specific number of products in combination to get a discount. Give these discount policies, give the number of each item, and ask how much money can be spent to take these items away?
Solution: Enter the number of item types, and then enter C, K, and unit price P for each item. Then, we will provide a discount policy for S. Each strategy has a num at the front, indicating the number and number of products to be combined, and then the num indicates the number and number of products to be given for each product, the final cost is the combination of fixed types and fixed numbers of products.
It is clear that the maximum number of products is 5, the maximum number of S is 99, and the maximum number of num is 5. Therefore, you can directly write a backpack... However, this backpack is 5-dimensional, and it is easy to write...
DP [I] [J] [k] [l] [m] indicates that the backpack is purchased by I, 0, and J, 1... Is initially set to I * (unit price I) + J * (price [J]) + K * (price [k]) +...
At each transfer, I, J, K, L, and m will transfer these s preferential policies... Minimum
if(i >= t0 && j >= t1 && k >= t2 && l >= t3 && m >= t4) { dp[i][j][k][l][m] = min(dp[i-t0][j-t1][k-t2][l-t3][m-t4] + a[o].p, dp[i][j][k][l][m]);}
The code is 120 + Ms...
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <ctime>#include <queue>#include <map>#include <sstream>#define CL(arr, val) memset(arr, val, sizeof(arr))#define REP(i, n) for((i) = 0; (i) < (n); ++(i))#define FOR(i, l, h) for((i) = (l); (i) <= (h); ++(i))#define FORD(i, h, l) for((i) = (h); (i) >= (l); --(i))#define L(x) (x) << 1#define R(x) (x) << 1 | 1#define MID(l, r) (l + r) >> 1#define Min(x, y) x < y ? x : y#define Max(x, y) x < y ? y : x#define E(x) (1 << (x))#define iabs(x) ((x) > 0 ? (x) : -(x))typedef long long LL;const double eps = 1e-8;const int inf = ~0u>>2;using namespace std;const int N = 110;struct node { int id[6], n[6]; int p, k;} a[N];struct pnode { int id, n, p;} b[6];int num[N*10];int dp[6][6][6][6][6];int main() { //freopen("data.in", "r", stdin); int t, i, s, x, y; int j, k, l, m, o, p, q; scanf("%d", &t); CL(b, 0); CL(a, 0); for(i = 0; i < t; ++i) { scanf("%d%d%d", &b[i].id, &b[i].n, &b[i].p); num[b[i].id] = i; } scanf("%d", &s); for(i = 0; i < s; ++i) { scanf("%d", &k); a[i].k = 0; while(k--) { scanf("%d%d", &x, &y); a[i].id[a[i].k] = num[x]; a[i].n[a[i].k++] = y; } scanf("%d", &p); a[i].p = p; } int t0, t1, t2, t3, t4; for(i = 0; i <= b[0].n; ++i) { for(j = 0; j <= b[1].n; ++j) { for(k = 0; k <= b[2].n; ++k) { for(l = 0; l <= b[3].n; ++l) { for(m = 0; m <= b[4].n; ++m) { dp[i][j][k][l][m] = i*b[0].p + j*b[1].p + k*b[2].p + l*b[3].p + m*b[4].p; for(o = 0; o < s; ++o) { t0 = t1 = t2 = t3 = t4 = 0; for(q = 0; q < a[o].k; ++q) { if(a[o].id[q] == 0) t0 = a[o].n[q]; if(a[o].id[q] == 1) t1 = a[o].n[q]; if(a[o].id[q] == 2) t2 = a[o].n[q]; if(a[o].id[q] == 3) t3 = a[o].n[q]; if(a[o].id[q] == 4) t4 = a[o].n[q]; } if(i >= t0 && j >= t1 && k >= t2 && l >= t3 && m >= t4) { //printf("%d %d %d %d %d\n", t0, t1, t2, t3, t4); dp[i][j][k][l][m] = min(dp[i-t0][j-t1][k-t2][l-t3][m-t4] + a[o].p, dp[i][j][k][l][m]); } } } } } } } printf("%d\n", dp[b[0].n][b[1].n][b[2].n][b[3].n][b[4].n]); return 0;}