zoukankan      html  css  js  c++  java
  • POJ1170 Shopping Offers <五维DP>

    Shopping Offers

    In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more customers, the shop introduces some special offers.
    A special offer consists of one or more product items for a reduced price. Examples: three flowers for 5 ICU instead of 6, or two vases together with one flower for 10 ICU instead of 12.
    Write a program that calculates the price a customer has to pay for certain items, making optimal use of the special offers. That is, the price should be as low as possible. You are not allowed to add items, even if that would lower the price.
    For the prices and offers given above, the (lowest) price for three flowers and two vases is 14 ICU: two vases and one flower for the reduced price of 10 ICU and two flowers for the regular price of 4 ICU.

    Input
    Your program is to read from standard input. The first line contains the number b of different kinds of products in the basket (0 <= b <= 5). Each of the next b lines contains three values c, k, and p. The value c is the (unique) product code (1 <= c <= 999). The value k indicates how many items of this product are in the basket (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). Notice that all together at most 5*5=25 items can be in the basket. The b+2nd line contains the number s of special offers (0 <= s <= 99). Each of the next s lines describes one offer by giving its structure and its reduced price. The first number n on such a line is the number of different kinds of products that are part of the offer (1 <= n <= 5). The next n pairs of numbers (c,k) indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are involved in the offer. The last number p on the line stands for the reduced price (1 <= p <= 9999). The reduced price of an offer is less than the sum of the regular prices.
    Output
    Your program is to write to standard output. Output one line with the lowest possible price to be paid.

    Sample Input
    2
    7 3 2
    8 2 5
    2
    1 7 3 5
    2 7 1 8 2 10
    Sample Output
    14

    标签:五维DP

    题目大意:有至多五种物品,给出每一种物品的单价和几种套餐的价格,求买目标物品至少需要多少钱。

    这是一道大水题。因为只有五种物品,可以用五维的dp来表示,五个维度分别表示五种物品的个数,即f[i][j][p][q][r]表示第一种物品取i个,第二种物品取j个......第五种物品取r个至少需要多少钱。把每种物品的单价看作一种只有一个物品的套餐,这样枚举套餐,看能否转移。

    AC代码如下:

    #include <iostream>
    #include <cstdio>
    #define MAX_N 5
    #define MAX_S 100
    #define MAX_C 1000
    #define INF 2147483647
    using namespace std;
    int n, m, cnt, basket[MAX_N+5], offer[MAX_S+10][MAX_N+5], v[MAX_S+10], map[MAX_C+5];
    int f[MAX_N+5][MAX_N+5][MAX_N+5][MAX_N+5][MAX_N+5];
    int main() {
    	scanf("%d", &n);
    	for (int i = 0; i < n; i++) {
    		int id, num, p;
    		scanf("%d%d%d", &id, &num, &p);
    		map[id] = i, basket[i] = num, offer[cnt][i] = 1, v[cnt++] = p;
    	}
    	scanf("%d", &m);
    	for (int i =0; i < m; i++) {
    		int num, id, k, p;
    		scanf("%d", &num);
    		for (int i = 0; i < num; i++) {
    			scanf("%d%d", &id, &k);
    			offer[cnt][map[id]] = k;
    		}
    		scanf("%d", &p);
    		v[cnt++] = p;
    	}
    	for (int a0 = 0; a0 <= basket[0]; a0++)
    		for (int a1 = 0; a1 <= basket[1]; a1++)
    			for (int a2 = 0; a2 <= basket[2]; a2++)
    				for (int a3 = 0; a3 <= basket[3]; a3++)
    					for (int a4 = 0; a4 <= basket[4]; a4++) {
    						if (a0 == 0 && a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0) {f[a0][a1][a2][a3][a4] = 0;	continue;}
    						f[a0][a1][a2][a3][a4] = INF;
    						for (int i = 0; i < cnt; i++) {
    							if (a0 < offer[i][0] || a1 < offer[i][1] || a2 < offer[i][2] || a3 < offer[i][3] || a4 < offer[i][4])	continue;
    							f[a0][a1][a2][a3][a4] = min(f[a0][a1][a2][a3][a4], f[a0-offer[i][0]][a1-offer[i][1]][a2-offer[i][2]][a3-offer[i][3]][a4-offer[i][4]]+v[i]);
    						}
    					}
    	printf("%d", f[basket[0]][basket[1]][basket[2]][basket[3]][basket[4]]);
    	return 0;
    }
    
  • 相关阅读:
    非标准的xml解析器的C++实现:二、解析器的基本构造:语法表
    非标准的xml解析器的C++实现:一、思考基本数据结构的设计
    lua5.4 beta中的to-be-closed变量的用法
    lua table与json的之间的互相转换高性能c++实现
    lua多线程共享数据的解决方案
    winsock完成端口套接字重用注意事项
    Less相关的用法以及Vue2.0 中如何使用Less
    1:MUI选择器组件抛出“n.getSelectedItem is not a function”异常的解决办法 2:mui三级联动 3:移动端关闭虚拟键盘
    redux状态管理和react-redux的结合使用
    初步学习React Router 4.0
  • 原文地址:https://www.cnblogs.com/AzraelDeath/p/7561775.html
Copyright © 2011-2022 走看看