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

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 641  Solved: 328
[Submit][Status]

Description

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

Input

    第1行输入N和日,之后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.

HINT

 

Source

题解:
呵呵。。。又是无限背包。。。费用取负,多算一点即可
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 50000+1000
14 #define maxm 5000+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 inline int read()
20 {
21     int x=0,f=1;char ch=getchar();
22     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
23     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
24     return x*f;
25 }
26 int n,m,f[maxn];
27 int main()
28 {
29     freopen("input.txt","r",stdin);
30     freopen("output.txt","w",stdout);
31     n=read();m=read();
32     memset(f,128,sizeof(f));
33     f[0]=0;
34     for(int i=1;i<=n;i++)
35     {
36         int x=read(),y=read();y=-y;
37         for(int j=x;j<=m+maxm;j++)f[j]=max(f[j],f[j-x]+y);  
38     }
39     int ans=-inf;
40     for(int i=m;i<=m+maxm;i++)ans=max(ans,f[i]);
41     printf("%d
",-ans);
42     return 0;
43 }
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/3941385.html