| ||||||||||
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> 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator