Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

多重背包(转化为完全背包再加上一个判断数组)

Posted by ldld at 2010-03-25 12:00:48 on Problem 1276
#include <iostream>

using namespace std;

bool f[100005];
int cnt[100005], a[15], b[15];

int main()
{
    int i, j, cash, n, temp;
    while(scanf("%d%d", &cash, &n) != EOF)
    {
        for(i = 0; i < n; i++)
                scanf("%d%d", &a[i], &b[i]);
        if(!cash || !n)
            printf("0\n");
        else
        {
            temp = 0;
            memset(f, 0, sizeof(f));
            f[0] = 1;
            for(i = 0; i < n; i++)
            {
                memset(cnt, 0, sizeof(cnt));
                for(j = b[i]; j <= cash; j++)
                {
                    if(!f[j] && f[j - b[i]] && cnt[j - b[i]] < a[i])
                    {
                        f[j] = 1;
                        cnt[j] = cnt[j - b[i]] + 1;
                        if(temp < j)
                            temp = j;
                    }
                }
            }
	        printf("%d\n", temp);
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator