BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草【01背包】

1618: [Usaco2008 Nov]Buying Hay 购买干草

Time Limit: 5 Sec Memory Limit: 64 MB

Description

约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号。第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5000)美元.每个干草公司的货源都十分充足,可以卖出无限多的干草包. 帮助约翰找到最小的开销来满足需要,即采购到至少H磅干草 .

Input

第1行输入N和H,之后N行每行输入一个Pi和Ci.

Output

最小的开销.

Sample Input

2 15
3 2
5 3

Sample Output

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

题解

这题裸的背包。

代码如下

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,Alv,Ans=1e9,Tot,Opt[100005];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d%d",&n,&Alv);Tot=Alv*2;
    memset(Opt,63,sizeof(Opt));Opt[0]=0;
    for(int i=1;i<=n;i++){
        int V,p;scanf("%d%d",&V,&p);
        for(int j=V;j<=Tot;j++)
        Opt[j]=min(Opt[j-V]+p,Opt[j]);
    }
    for(int i=Alv;i<=Tot;i++) Ans=min(Opt[i],Ans);
    printf("%d\n",Ans);
    return 0;
}
posted @ 2018-05-15 18:10  XSamsara  阅读(115)  评论(0编辑  收藏  举报