The main topic: there is a person to buy B goods, give the number of each item, price and quantity, coincides with the store discount, there is a discount way. Ask how to make the price of buy to reach the lowest
Solution thinking: Up to five products, and each item up to only 5, so you can use 5-D DP to represent, each dimension represents the quantity of one item
Discount in fact there is a B + s species, will be a single piece of each product sold is also a discount way
There's a hole in the question, B or S, it could be 0.
#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace STD;#define MAXN 6#define MAXS#define INF 0x3f3f3f3fstructproduct{intC, K, p;} PRO[MAXN];structoffer{intNUM[MAXN], Price;} OFF[MAXS];intb, S;intDP[MAXN][MAXN][MAXN][MAXN][MAXN]; map<int,int>MvoidInit () {memset(DP,-1,sizeof(DP));memset(Off,0,sizeof(off)); M.clear (); for(inti =0; I < b; i++) {scanf("%d%d%d", &PRO[I].C, &PRO[I].K, &PRO[I].P); M[PRO[I].C] = i; Off[i].num[i] =1; Off[i].price = PRO[I].P; }if(b <5) { for(inti = b; I <5; i++) PRO[I].K = PRO[I].P =0; }scanf("%d", &s);intN, c, K; for(inti = b; I < b + S; i++) {scanf("%d", &n); for(intj =0; J < N; J + +) {scanf("%d%d", &c, &k); OFF[I].NUM[M[C]] + = k; }scanf("%d", &off[i].price); }}intDfsintA1,intA2,intA3,intA4,intA5) {if(DP[A1][A2][A3][A4][A5]! =-1)returnDP[A1][A2][A3][A4][A5]; DP[A1][A2][A3][A4][A5] = INF; for(inti =0; I < b + S; i++) {if(A1 >= off[i].num[0] && A2 >= off[i].num[1] && A3 >= off[i].num[2] && A4 >= off[i].num[3] && a5 >= off[i].num[4]) Dp[a1][a2][a3][a4][a5] = min (Dp[a1][a2][a3][a4][a5], Off[i].price + DFS (a1-off[i].num[0],a2-off[i].num[1],a3-off[i].num[2],a4-off[i].num[3],a5-off[i].num[4])); }returnDP[A1][A2][A3][A4][A5];}voidSolve () {dp[0][0][0][0][0] =0;if(b = = B + s) {intt =0; for(inti =0; I <5; i++) T + = PRO[I].P * PRO[I].K;printf("%d\n", t); }Else printf("%d\n", DFS (pro[0].K, pro[1].K, pro[2].K, pro[3].K, pro[4].K));}intMain () {scanf("%d", &b); Init (); Solve ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ-1170 Shopping offers (five D DP)