C - Coins (multiple knapsack problem)

The sky and night are as cool as water, lying down to watch Altair Vega.

The Qixi Festival is here, and LFgg is going to give NPY a gift. However, LFgg is a perfectionist obsessive, and he can't stand others giving him change when buying gifts.
LFgg has many banknotes of different denominations. He hopes you can write a program to see how many prices he can pay in the range of 1 to m.

Input

The input contains sets of examples.
The first row of each set of samples contains two positive integers n(1 ≤ n ≤ 100), m(m ≤ 100000).
The second row contains 2n integers, A1,A2,A3….An,C1,C2,C3….Cn(1 ≤ Ai ≤ 100000, 1 ≤ Ci ≤ 1000).
Ai represents the face value of each banknote, and Ci represents the number of such banknotes owned by LFgg.
Data ends with 0 0.

Output

Output an integer for each group of samples, representing the number of price types that can be paid within the range of 1 to m.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long ll;
const int N = 110, M = 100010;

int n, m;
int v[N], s[N];
int f[M], g[M];

int main()
{
	while (cin >> n >> m, n || m)
	{
		for (int i = 1; i <= n; i++) cin >> v[i];
		for (int i = 1; i <= n; i++) cin >> s[i];

		memset(f, 0, sizeof f);

		f[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			memset(g, 0, sizeof g);
			for (int j = v[i]; j <= m; j++)
				if (!f[j] && f[j - v[i]] && g[j - v[i]] < s[i])
				{
					f[j] = 1;
					g[j] = g[j - v[i]] + 1;
				}
		}

		int res = 0;
		for (int i = 1; i <= m; i++) res += f[i];
		cout << res << endl;
	}

	return 0;
}

 

Guess you like

Origin blog.csdn.net/GF0919/article/details/132411794