【bzoj1618】【Usaco2008 Nov]】Buying Hay 购买干草

题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1618
题解:
很明显的dp,背包dp
代码:

#include
#include
#include
#define maxn (55005)
using namespace std;
int n,m,w[maxn],c[maxn],dp[maxn+500];
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=maxn+400;i++)
    dp[i]=9999999;
    for (int i=1;i<=n;i++)
    scanf("%d%d",&w[i],&c[i]);

    dp[0]=0;
    for (int i=1;i<=n;i++)
    for (int j=0;j<=maxn+400;j++)
    {
        if (dp[j]!=9999999&&j+w[i]<=maxn+400)
        dp[j+w[i]]=min(dp[j+w[i]],dp[j]+c[i]);
    }
    int ans=9999999;
    for (int j=maxn+400;j>=m;j--)
    ans=min(ans,dp[j]);
    cout<

你可能感兴趣的:(bzoj)