[USACO08NOV] Buying Hay S(完全背包+背包容积扩大)

[USACO08NOV] Buying Hay S

题目描述

Farmer John is running out of supplies and needs to purchase H (1 <= H <= 50,000) pounds of hay for his cows.

He knows N (1 <= N <= 100) hay suppliers conveniently numbered 1…N. Supplier i sells packages that contain P_i (1 <= P_i <= 5,000) pounds of hay at a cost of C_i (1 <= C_i <= 5,000) dollars. Each supplier has an unlimited number of packages available, and the packages must be bought whole.

Help FJ by finding the minimum cost necessary to purchase at least H pounds of hay.

约翰的干草库存已经告罄,他打算为奶牛们采购 H ( 1 ≤ H ≤ 50000 ) H(1 \leq H \leq 50000) H(1H50000) 磅干草。

他知道 N ( 1 ≤ N ≤ 100 ) N(1 \leq N\leq 100) N(1N100) 个干草公司,现在用 1 1 1 N N N 给它们编号。第 i i i 公司卖的干草包重量为 P i ( 1 ≤ P i ≤ 5 , 000 ) P_i (1 \leq P_i \leq 5,000) Pi(1Pi5,000) 磅,需要的开销为 C i ( 1 ≤ C i ≤ 5 , 000 ) C_i (1 \leq C_i \leq 5,000) Ci(1Ci5,000) 美元。每个干草公司的货源都十分充足, 可以卖出无限多的干草包。

帮助约翰找到最小的开销来满足需要,即采购到至少 H H H 磅干草。

输入格式

* Line 1: Two space-separated integers: N and H

* Lines 2…N+1: Line i+1 contains two space-separated integers: P_i and C_i

输出格式

* Line 1: A single integer representing the minimum cost FJ needs to pay to obtain at least H pounds of hay.

样例 #1

样例输入 #1

2 15 
3 2 
5 3

样例输出 #1

9

提示

FJ can buy three packages from the second supplier for a total cost of 9.

思路

这道题其实很容易,但它说帮助约翰找到最小的开销来满足需要,即采购到至少 H H H 磅干草。因此,这里的背包容量没有上限,所以可能在容量范围内没有最优解,处理的方式是强行将背包容量扩大。

代码

//完全背包

#include<iostream>
#include<cstring>

#define int long long

using namespace std;

const int N = 1e6+10;

int f[N];
int V,n;

signed main(){
    cin>>n>>V;
    
    memset(f,0x3f,sizeof f);
    
    f[0]=0;
    
    for(int i=1;i<=n;i++){
        int v,w;
        cin>>v>>w;
        for(int j=v;j<=V+5000;j++){
            //注意循环结束为m+5000,因为你只购买m千克时花费的钱不一定是最少的,5000时一坨草质量的最大值 
            f[j]=min(f[j],f[j-v]+w);
        }
    }
    
    int res=0x3f3f3f3f;
    
    for(int i=V;i<=V+5000;i++){
        res=min(res,f[i]);
    }
    
    cout<<res;
    
    return 0;
}
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值