USACO Buying Hay
洛谷 P2918 [USACO08NOV]买干草Buying Hay
https://www.luogu.org/problem/P2918
JDOJ 2592: USACO 2008 Nov Silver 1.Buying Hay
https://neooj.com:8082/oldoj/problem.php?id=2592
题目描述
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 \leq H \leq 50000)H(1≤H≤50000)镑干草.
他知道N(1 \leq N\leq 100)N(1≤N≤100)个干草公司,现在用11到NN给它们编号.第ii公司卖的干草包重量 为P_i (1 \leq P_i \leq 5,000)Pi(1≤Pi≤5,000) 磅,需要的开销为C_i (1 \leq C_i \leq 5,000)Ci(1≤Ci≤5,000) 美元.每个干草公司的货源都十分充足, 可以卖出无限多的干草包.
帮助约翰找到最小的开销来满足需要,即采购到至少HH镑干草.
输入格式
* Line 1: Two space-separated integers: N and H
* Lines 2..N+1: Line i+1 contains two space-separated integers: P_i and C_i
输出格式
* Line 1: A single integer representing the minimum cost FJ needs to pay to obtain at least H pounds of hay.
输入输出样例
2 15 3 2 5 3
9
说明/提示
FJ can buy three packages from the second supplier for a total cost of 9.
裸的完全背包。
出了一些小bug。
我FSW以后要是再用memset置数组,我当场吃翔。
这道题一个坑点就是,你可以多买一些草,不一定非得是h,因为公司提供的是干草包,而干草包最多有5000,所以需要把范围多置5000,这是细节。
然后就是纯套完全背包模板。
祝大家刷题愉快。
#include<cstdio> #include<algorithm> using namespace std; int n,h,ans=1e9;//n,种类数,h,总体积 int m[101],w[101]; int dp[55001]; int main() { for(int i=1;i<=55001;i++) dp[i]=1e9; scanf("%d%d",&n,&h); for(int i=1;i<=n;i++) scanf("%d%d",&m[i],&w[i]); for(int i=1;i<=n;i++) for(int j=m[i];j<=h+5000;j++) dp[j]=min(dp[j],dp[j-m[i]]+w[i]); for(int i=h;i<=h+5000;i++) ans=min(ans,dp[i]); printf("%d",ans); return 0; }