[Usaco2008 Nov]Buying Hay 购买干草
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1241 Solved: 646
[ Submit][ Status][ Discuss]
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
3 2
5 3
Sample Output
9
FJ can buy three packages from the second supplier for a total cost of 9.
FJ can buy three packages from the second supplier for a total cost of 9.
HINT
完了,完全背包都写错了
#include<bits/stdc++.h>
using namespace std;
int h,n,ans=0x7fffffff,p[105],c[105],f[60005],mx;
int main(){
memset(f,127,sizeof(f)); f[0] = 0;
scanf("%d%d", &n, &h);
for( int i = 1; i <= n; i++ ) scanf("%d%d", &p[i], &c[i]), mx = max(mx,c[i]);
for( int j = 1; j <= n; j++ )
for( register int i = 1; i <= h+mx; i++ )
if( i >= p[j] ) f[i] = min( f[i], f[i-p[j]] + c[j] );
for( int i = h; i <= h+mx; i++ ) ans = min( ans, f[i] );
printf("%d\n", ans);
return 0;
}