牛客-购买干草(完全背包)
作者:互联网
https://ac.nowcoder.com/acm/problem/24979
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=6e5+10;
const int mod=1e9+7;
int dp[maxn],p[maxn],c[maxn];
//完全背包,但空间上限增加一个pi的范围,所以答案范围在dp[h]~dp[h+5000]之间
int main(){
int n,h;
cin>>n>>h;
for(int i=1;i<=n;i++)cin>>p[i]>>c[i];
memset(dp,inf,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
for(int j=c[i];j<=h+5000;j++)
dp[j]=min(dp[j-p[i]]+c[i],dp[j]);
int ans=inf;
for(int i=h;i<=h+5000;i++) ans=min(ans,dp[i]);
cout<<ans;
}
标签:背包,const,int,牛客,maxn,inf,include,干草,dp 来源: https://blog.csdn.net/Joekiccer/article/details/123101791