F - Shopping Offers POJ - 1170 (状压+背包)

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

思路:题目转化一下就是有n+m种商品,可以无限买,但是要在花费最小的情况下买够清单的物品。

因为物品可能会有5种,所以用单纯的背包维度无法控制,所以考虑将物品进行状态压缩,每种物品对应一种6进制数(之所以不用2进制是因为二进制只能表示选与不选,而这里有数量限制)。

进行背包时,因为必需要选够,所以初始化为inf。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
int n,m;
int six[10]={1,6,36,216,1296,7776,46656,279936};
int dp[maxn];
int Hash[maxn];
struct Point
{
    int has,val,num;
}p[maxn];
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d",&n)!=EOF)
    {
        memset(Hash,-1,sizeof(Hash));
        int ans=0,Num=0;
        for(int i=0;i<n;i++)
        {
            int id;
            scanf("%d%d%d",&id,&p[i].num,&p[i].val);
            p[i].has=six[i];
            Hash[id]=six[i];
            ans+=p[i].num*p[i].val;
            Num+=p[i].num*six[i];
            p[i].num=six[i];
        }
        scanf("%d",&m);
        int tmp=m;
        int i=0;
        while(tmp--)
        {
            int k;
            scanf("%d",&k);
            while(k--)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                if(Hash[x]==-1) continue;
                p[i+n].num+=Hash[x]*y;
            }
            scanf("%d",&p[i+n].val);
            i++;
        }
        memset(dp,INF,sizeof(dp));
        dp[0]=0;
        for(int i=0;i<m+n;i++)
        {
            for(int j=p[i].num;j<=Num;j++)
            {
                dp[j]=min(dp[j-p[i].num]+p[i].val,dp[j]);
            }
        }
        printf("%d\n",min(ans,dp[Num]));
    }
    return 0;
}

 

POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值