POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)

时间:2022-09-21 10:00:55

题目地址:http://poj.org/problem?id=1170

Description

POJ 1170 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

谨以此来纪念蛋疼六重循环!!!

#include <stdio.h>
#include <string.h>
#include <limits.h> int code[6]; //商品代码
int num[6]; //商品数量
int price[6]; //商品价格
int special_num[100][6]; //促销项目各个商品数量
int special_cnt[100]; //促销项目的商品数量
int special_price[100]; //促销项目价格
int basket;
int special;
int dp[6][6][6][6][6]; int Decode (int c){
int i;
for (i=1; i<=5; ++i){
if (code[i] == c)
break;
}
return i;
} void Init(){
int i;
int j;
int c;
int k;
int index; scanf ("%d", &basket);
for (i=1; i<=basket; ++i){
scanf ("%d%d%d", &code[i], &num[i], &price[i]);
}
scanf ("%d", &special);
for (i=1; i<=special; ++i){
scanf ("%d", &special_cnt[i]);
for (j=1; j<=special_cnt[i]; ++j){
scanf ("%d%d", &c, &k);
index = Decode (c);
special_num[i][index] = k;
}
scanf ("%d", &special_price[i]);
}
} void Lowest_Price (){
int i1, i2, i3, i4, i5;
int i;
int tmp1, tmp2;
memset (dp, -1, sizeof(dp));
dp[0][0][0][0][0] = 0;
for (i1=0; i1<=num[1]; ++i1){
for (i2=0; i2<=num[2]; ++i2){
for (i3=0; i3<=num[3]; ++i3){
for (i4=0; i4<=num[4]; ++i4){
for (i5=0; i5<=num[5]; ++i5){
tmp1 = INT_MAX;
tmp2 = INT_MAX;
for (i=1; i<=special; ++i){
if (i1 >= special_num[i][1] &&
i2 >= special_num[i][2] &&
i3 >= special_num[i][3] &&
i4 >= special_num[i][4] &&
i5 >= special_num[i][5]){
tmp2 = dp[i1-special_num[i][1]]
[i2-special_num[i][2]]
[i3-special_num[i][3]]
[i4-special_num[i][4]]
[i5-special_num[i][5]] + special_price[i];
if (tmp1 > tmp2)
tmp1 = tmp2;
}
}
if (tmp1 != INT_MAX){
dp[i1][i2][i3][i4][i5] = tmp1;
}
else{
dp[i1][i2][i3][i4][i5] = i1 * price[1] + i2 * price[2]
+ i3 * price[3] + i4 * price[4] + i5 * price[5];
}
}
}
}
}
}
printf ("%d\n", dp[num[1]][num[2]][num[3]][num[4]][num[5]]);
} int main(void){
Init ();
Lowest_Price (); return 0;
}

POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)的更多相关文章

  1. 背包系列练习及总结(hud 2602 &amp&semi;&amp&semi; hdu 2844 Coins &amp&semi;&amp&semi; hdu 2159 &amp&semi;&amp&semi; poj 1170 Shopping Offers &amp&semi;&amp&semi; hdu 3092 Least common multiple &amp&semi;&amp&semi; poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

  2. poj 1170 Shopping Offers

    Shopping Offers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4696   Accepted: 1967 D ...

  3. POJ 1170 Shopping Offers非状态压缩做法

    Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...

  4. poj - 1170 - Shopping Offers(减少国家dp)

    意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...

  5. POJ 1170 Shopping Offers(完全背包&plus;哈希)

    http://poj.org/problem?id=1170 题意:有n种花的数量和价格,以及m种套餐买法(套餐会便宜些),问最少要花多少钱. 思路:题目是完全背包,但这道题目不好处理的是套餐的状态, ...

  6. POJ - 1170 Shopping Offers &lpar;五维DP&rpar;

    题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...

  7. HDU 1170 Shopping Offers 离散&plus;状态压缩&plus;完全背包

    题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...

  8. Week 9 - 638&period;Shopping Offers - Medium

    638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...

  9. 洛谷P2732 商店购物 Shopping Offers

    P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...

随机推荐

  1. 设计模式(四)抽象工厂模式(Abstract Factory Pattern)

    一.引言 在上一专题中介绍了工厂方法模式,工厂方法模式是为了克服简单工厂模式的缺点而设计出来的,简单工厂模式的工厂类随着产品类的增加需要增加额外的代码,而工厂方法模式每个具体工厂类只完成单个实例的创建 ...

  2. android 内存不足的问题

    FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ' ...

  3. python和tk实现桌面番茄时间(1)

    参考资料: An Introduction To Tkinter Python 定时器 Python实例讲解 -- 定时播放 (闹钟+音乐) Python Tkinter之Label #coding= ...

  4. 完整的拆分nginx访问日志

    <pre name="code" class="html"> 10.168.255.134 [09/Oct/2016:15:28:52 +0800] ...

  5. javascript深入理解js闭包(个人理解,大神勿喷)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  6. angular自定义验证 ngModel的一些理解

    每次使用自定义校验都不记得具体详情,故而记录之 1.数据流向 初始化 -->$formatters -->modelValue-->用户操作-->viewValue--> ...

  7. TensorFlow学习笔记(一):数据操作指南

    扩充 TensorFlow tf.tile 对数据进行扩充操作 import tensorflow as tf temp = tf.tile([1,2,3],[2]) temp2 = tf.tile( ...

  8. 【unix网络编程第三版】阅读笔记(二):套接字编程简介

    unp第二章主要将了TCP和UDP的简介,这些在<TCP/IP详解>和<计算机网络>等书中有很多细致的讲解,可以参考本人的这篇博客[计算机网络 第五版]阅读笔记之五:运输层,这 ...

  9. JavaScript手工编写滚动条组件

    0 前言 上周的一个练习,由于没来得及编写笔记,这里补充一下~ 虽然CSS3中提供了overflow:scroll; 来实现滚动条,但是这里可以使用原生JS来编写一个,以达到练习组件编写的效果. 练习 ...

  10. TF-IDF算法(2)—python实现

    参加完数模之后休息了几天,今天继续看TF-IDF算法.上篇中对TF-IDF算法已经做了详细的介绍,在此不再赘述.今天主要是通过python,结合sklearn库实现该算法,并通过k-means算法实现 ...