洛谷P2918 [USACO08NOV]买干草Buying Hay

题目描述

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.

约翰的干草库存已经告罄,他打算为奶牛们采购丹(1 <=H< =50000)镑干草.

他知道N(l<=N<=100)个干草公司,现在用1到N给它们编号.第公司卖的干草包重量 为巧P_i (1 <= P_i <= 5,000) 磅,需要的开销为C_i (1 <= C_i <= 5,000) 美元.每个干草公司的货源都十分充足, 可以卖出无限多的干草包.

帮助约翰找到最小的开销来满足需要,即采购到至少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: 
2 15 
3 2 
5 3 
输出样例#1: 
9 

说明

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

完全背包水题。。。

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 110
#define MAXM 60010
using namespace std;
int n,m,ans=999999999,v[MAXN],w[MAXN],f[MAXM];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d%d",&w[i],&v[i]);
    for(int i=1;i<=MAXM;i++)f[i]=999999999;
    for(int i=1;i<=n;i++)
    for(int j=w[i];j<=m+10000;j++)
    f[j]=min(f[j],f[j-w[i]]+v[i]);
    for(int i=m;i<=m+10000;i++)
    ans=min(ans,f[i]);
    printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值