dp 01背包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Let_life_stop/article/details/82873509

题目链接:https://cn.vjudge.net/contest/68966#problem/F

AC代码(没事不要交g++,有c++一定要交c++):

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<map>
using namespace std;
# define maxn 10010+10
# define inf 0x3f3f3f3f
# define ll long long
int dp[maxn];
int  n;
struct node
{
    int pr;
    int we;
} q[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int temp=m-n;
        for(int i=0; i<=temp; i++)
            dp[i]=inf;
        int t;
        scanf("%d",&t);
        dp[0]=0;
        for(int i=1; i<=t; i++)
        {
            scanf("%d%d",&q[i].pr,&q[i].we);
        }

        for(int i=1; i<=t; i++)
        {
            for(int j=q[i].we; j<=temp; j++)
            {
                dp[j]=min(dp[j-q[i].we]+q[i].pr,dp[j]);
            }
        }
        if(dp[m-n]==inf)printf("This is impossible.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m-n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Let_life_stop/article/details/82873509