序号:1854
[USACO08NOV]Buying Hay S
Time Limit:1s Memory Limit:125MB
题目描述
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.
约翰的干草库存已经告罄,他打算为奶牛们采购 H(1≤H≤50000)磅干草。
他知道 N(1≤N≤100)个干草公司,现在用 1 到 N 给它们编号。第 i 公司卖的干草包重量为 (1≤
≤5,000) 磅,需要的开销为
(1≤
≤5,000)美元。每个干草公司的货源都十分充足, 可以卖出无限多的干草包。
帮助约翰找到最小的开销来满足需要,即采购到至少 HH 磅干草。
输入格式
* Line 1: Two space-separated integers: N and H
第1行:两个以空格分隔的整数:N和H
* Lines 2..NN+1: Lineii+1 contains two space-separated integers:
and
第2..N+1行:包含两个以空格分隔的整数:和
输出格式
* Line 1: A single integer representing the minimum cost FJ needs to pay to obtain at least HH pounds of hay.
第1行:一个整数代表约翰购买 H 磅干草所需要的最小开销
输入输出样例
输入 #1
2 15 3 2 5 3
输出 #1
9
说明/提示
FJ can buy three packages from the second supplier for a total cost of 9.
约翰可以从第二个干草公司购买三包干草总共花费9美元
#include<iostream>
#include<bits/stdc++.h>
#define MAXN 110
#define MAXM 60010
#define ll long long
using namespace std;
ll n, m, ans = 999999999, v[MAXN], w[MAXN], f[MAXM];
int main() {
scanf("%d%d", &n, &m);
for (ll i = 1; i <= n; i++)scanf("%d%d", &w[i], &v[i]);
for (ll i = 1; i <= MAXM; i++)f[i] = 999999999;
for (ll i = 1; i <= n; i++)
for (ll j = w[i]; j <= m + 10000; j++)
f[j] = min(f[j], f[j - w[i]] + v[i]);
for (ll i = m; i <= m + 10000; i++)
ans = min(ans, f[i]);
printf("%d", ans);
return 0;
}