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 2007011268 at 2012-08-12 13:54:17 on Problem 1276
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 11;
const int Cash = 100001;
int res[Cash];
int cnt[Cash];
int num[N];
int val[N];

int get_res(int cash, int n)
{
	if(cash == 0 || n == 0)
		return 0;

	memset(res, 0, sizeof(res));
	for(int i=1; i<=n; i++)
	{
		memset(cnt, 0, sizeof(cnt));
		for(int j=1; j<=cash; j++)
		{
			if(j - val[i] >= 0 && cnt[j - val[i]] < num[i] && res[j] < res[j-val[i]] + val[i])
			{
				res[j] = res[j-val[i]] + val[i];
				cnt[j] = cnt[j-val[i]] + 1;
			}
		}
	}
	return res[cash];
}

int main()
{
	int n, cash;
	while(scanf("%d%d", &cash, & n) != EOF)
	{
		for(int i=1; i<=n; i++)
			scanf("%d%d", &num[i], &val[i]);
		int tmp = get_res(cash, n);
		printf("%d\n", tmp);
	}
	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