/*
* 我認為,首先求出能支付的錢的特定數目(多重背包)
* 之後在上面的基礎上對找錢進行完全背包,即可解決
* I think that first obtained the money can be paid a certain number (multiple backpack)
* After completely backpack on the basis of the above, give change, can be solved
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXV 1200005
#define MAXN 101
#define INF 0x3f3f3f3f
int val[MAXN], cnt[MAXN], dp[MAXV];
void complete_knapsack(int max_v, int x, int step)
{
for(int v = 0; v <= max_v-x; v ++) {
if( INF != dp[v] ) {
dp[v+x] = min(dp[v+x], dp[v]+step);
}
}
}
void zero_one_knapsack(int max_v, int x, int step)
{
for(int v = max_v-x; v >= 0; v --) {
if( INF != dp[v] ) {
dp[v+x] = min(dp[v+x], dp[v]+step);
}
}
}
void special_knapsack(int max_v, int x, int step)
{
for(int v = max_v; v >= x; v --) {
if( INF != dp[v] ) {
dp[v-x] = min(dp[v-x], dp[v]+step);
}
}
}
int dynamic_programming(int n, int ans)
{
int binary, t_cnt, max_v(10000);
fill(dp, dp+max_v+2, INF); dp[0] = 0;
for(int i = 0; i < n; i ++) {
if( cnt[i]*val[i] >= max_v ) {
complete_knapsack(max_v, val[i], 1);
}
else {
binary = 1; t_cnt = cnt[i];
while( binary < t_cnt ) {
zero_one_knapsack(max_v, binary*val[i], binary);
t_cnt -= binary;
binary <<= 1;
}
zero_one_knapsack(max_v, t_cnt*val[i], t_cnt);
}
}
for(int i = 0; i < n; i ++) {
special_knapsack(max_v, val[i], 1);
}
return (INF == dp[ans])? -1 : dp[ans];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, max_v, ans;
while( ~scanf("%d %d", &n, &ans) ) {
for(int i = 0; i < n; i ++) {
scanf("%d", &val[i]);
}
for(int i = 0; i < n; i ++) {
scanf("%d", &cnt[i]);
}
printf("%d\n", dynamic_programming(n, ans));
}
return 0;
}
poj_3260
最新推荐文章于 2022-08-02 11:49:41 发布