POJ-1170 Shopping offers (five D DP)

Source: Internet
Author: User

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", &AMP;PRO[I].C, &AMP;PRO[I].K, &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.