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;
    }
    
  • 相关阅读:
    (五)SpringCloud学习系列-构建部门微服务消费者Module
    (四)SpringCloud学习系列-构建部门微服务提供者Module
    (三)SpringCloud学习系列-Rest微服务构建
    git提交 显示作者名不是自己
    linux一些常见命令
    支付宝退款demo
    ffmpeg截取视频
    excel导入数据到mysql
    二分法与冒泡排序
    mysql的级联删除
  • 原文地址:https://www.cnblogs.com/AzraelDeath/p/7561775.html
Copyright © 2011-2022 走看看