POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)
题目地址:http://poj.org/problem?id=1170
Description
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
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
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 -- 动态规划(虐心的六重循环啊!!!)的更多相关文章
- 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)
作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...
- poj 1170 Shopping Offers
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4696 Accepted: 1967 D ...
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
- poj - 1170 - Shopping Offers(减少国家dp)
意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...
- POJ 1170 Shopping Offers(完全背包+哈希)
http://poj.org/problem?id=1170 题意:有n种花的数量和价格,以及m种套餐买法(套餐会便宜些),问最少要花多少钱. 思路:题目是完全背包,但这道题目不好处理的是套餐的状态, ...
- POJ - 1170 Shopping Offers (五维DP)
题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
- Week 9 - 638.Shopping Offers - Medium
638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...
- 洛谷P2732 商店购物 Shopping Offers
P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...
随机推荐
- SMP和MAPP的区别
SMP(Symmetrical Multi-Processing),对称多处理系统,是指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构.它是相对非对称多处理技术而言 ...
- 写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseList
package homework004; import java.util.ArrayList; import java.util.List; public class Daoxu { public ...
- mac下烦人的eclipse安装svn插件
eclipse作为一个鸡肋般的java ide,颇有食之无味弃之可惜之感.最近公司统一对电脑做了一些处理,搞的我的eclipse都不能用了.重新安装了一下,各种maven.svn,代码格式什么的依赖神 ...
- linux tomcat自启动设置
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- MSSQL 如何实现 MySQL 的 limit 查询方式 (转)
不知为何,MSSQL 中没有 limit 这个极为重要的查询方式,熟悉 MySQL 的朋友都知道,MySQL 的 limit 对于实现分页和一些限制结果集的应用中非常方便.没有不要紧,我们可以用其他方 ...
- 分析Model2系统心得
分析Model2系统心得 前言:观摩他人的项目,学到一些新的.实践经验呀!!! 1. 怎样使用字符串处理类?从页面获取的Form类或者字段取值时使用. 2.在验证用户身份时,先推断username, ...
- C# 创建、安装和卸载Windows服务程序
1.新建一个windows服务程序. 2.点击这个服务类,从工具箱中加入一个Timer控件,右键这个Timer控件 命名为 timerOrderDeductionDetailJob,Enable设为T ...
- [React Fundamentals] State Basics
State is used for properties on a component that will change, versus static properties that are pass ...
- 用C++程序理解汉字的机内码表示
汉字的编码是非常多刚開始学习的人不easy搞不明确的事情.最早的汉字字符集是GB2312-80,收入汉字6763个,符号715个,总计7478个字符,大陆普遍使用的简体字字符集.本文借助于一个能输出这 ...
- python抓取伯乐在线的全部文章,对标题分词后存入mongodb中
依赖包: 1.pymongo 2.jieba # -*- coding: utf-8 -*- """ @author: jiangfuqiang "" ...