\[ dp[j+c[i]]=Min(dp[j+c[i]],dp[j]+v[i]) \]
\[ dp[Min(j+c[i],m)]=Min(dp[Min(j+c[i]),m],dp[j]+v[i]) \]
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 101
#define maxm 50001
using namespace std;
int dp[maxm],c[maxn],v[maxn];
int n,m;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int main(){
n=read(),m=read();
for(register int i=1;i<=n;i++) c[i]=read(),v[i]=read();
memset(dp,0x3f,sizeof dp);
dp[0]=0;
for(register int i=1;i<=n;i++){
for(register int j=0;j<=m;j++){
dp[min(j+c[i],m)]=min(dp[min(j+c[i],m)],dp[j]+v[i]);
}
}
printf("%d\n",dp[m]);
return 0;
}
[Usaco2008 Nov]Buying Hay 购买干草
原文:https://www.cnblogs.com/akura/p/10864467.html