| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
转化为完全背包问题,背包九讲给力啊#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator