【BZOJ】1618: [Usaco2008 Nov]Buying Hay 购买干草(dp)

http://www.lydsy.com/JudgeOnline/problem.php?id=1618

裸的01背包,注意背包的容量不是v即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=105, oo=~0u>>1;
int f[50005+5005], v[N], w[N], n, V;

int main() {
	read(n); read(V);
	int mx=0;
	for1(i, 1, n) { read(v[i]); read(w[i]); mx=max(v[i], mx); }
	for1(i, 1, V+mx) f[i]=oo>>1;
	for1(i, 1, n) {
		for(int j=v[i]; j<=mx+V; ++j)
			f[j]=min(f[j], f[j-v[i]]+w[i]);
	}
	int ans=oo;
	for(int j=V; j<=mx+V; ++j) ans=min(ans, f[j]);
	print(ans);
	return 0;
}

 

 


 

 

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

posted @ 2014-09-02 13:57  iwtwiioi  阅读(394)  评论(0编辑  收藏  举报