poj 1276 Cash Machine

来源:互联网 发布:独立游戏 知乎 编辑:程序博客网 时间:2024/06/05 16:04

多重背包的其他应用

#include<iostream>#include<string.h>#include<math.h>#include<fstream>#include<algorithm>#include<stdio.h>#include<queue>#include<vector> #define MAXSIZE 100using namespace std;int cash = 0, N = 0, n[11], d[11]; int dp[100001];int rec[100001]; int ans = 0;int main(){    //freopen("data_1276.txt","r",stdin);    while(scanf("%d%d", &cash, &N) != EOF)    {        for (int i = 1; i <= N; i++)        {            scanf("%d%d", &n[i], &d[i]);        }        memset(dp, 0, sizeof(dp));        dp[0] = 1;         for (int i = 1; i <= N; i++)        {            memset(rec, 0, sizeof(rec));            for (int j = d[i]; j <= cash; j++)            {                if (!dp[j] && dp[j - d[i]] && rec[j - d[i]] < n[i])                {                    dp[j] = 1;                    rec[j] = rec[j - d[i]] + 1;                  }            }         }        while(!dp[cash] && cash > 0)        {            cash--;        }        printf("%d\n", cash);    }    return 0;}
0 0
原创粉丝点击