/*Personal Opinion: I really think poj is more classic than HDU !!! More and more think this way !!!
The same was said yesterday !!! It seems that the center of gravity needs to be shifted !!! */
This is a multi-bag question. It is very good. You can simply change it and you will know that this question is good !!!
Admire those experts who have made questions !!! Haha ·····
Multiple backpack templates :(The template has just been typed. Check whether the template is correct or not.)
@ 1:
# Include <stdio. h>
# Include <string. h>
# Define max (A, B) A> B? A: B
Int vv;
Int BB [10000];
Void Ni (int w, int V)
{
Int I;
For (I = vv; I> = V; I ++)
Bb [I] = max (BB [I], BB [I-v] + W );
}
Void Shun (int w, int V)
{
Int I;
For (I = V; I <= vv; I ++)
Bb [I] = max (BB [I], BB [I-v] + W );
}
Int main ()
{
Int N, amount [100], W [100], V [100], K, I;
Scanf ("% d", & N, & vv );
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I], & V [I], & amount [I]);
Memset (BB, 0, sizeof (bb ));
For (I = 0; I <n; I ++)
{
If (amount [I] * V [I]> = vv)
{
Shun (W [I], V [I]);
}
Else
{
K = 1;
While (amount [I]> K)
{
Amount [I]-= K;
Ni (K * W [I], K * V [I]);
K * = 2;
}
Ni (amount [I] * W [I], amount [I] * V [I]);
}
}
Printf ("% d \ n", BB [VV]);
Return 0;
}
@ 2: (this triple loop is not good)
# Include <stdio. h>
# Include <string. h>
# Define max (A, B) A> B? A: B
Int vv;
Int BB [10000];
Int main ()
{
Int N, amount [100], W [100], V [100], K, I, J;
Scanf ("% d", & N, & vv );
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I], & V [I], & amount [I]);
Memset (BB, 0, sizeof (bb ));
For (I = 0; I <n; I ++)
For (j = vv; j> = V [I]; j --)
For (k = 1; k <amount [I] & J> = amount [I] * k; k ++)
Bb [J] = max (BB [J], BB [J-K * V [I] + K * W [I]);
Printf ("% d \ n", BB [VV]);
Return 0;
}
*/
Code:
# Include <stdio. h>
# Include <string. h>
# Define INF 99999999
# Define max (A, B) A> B? A: B
# Define min (a, B) a <B? A: B
Int main ()
{
Int VV, N, I, J, K, ans;
Int bb1 [20000], BB2 [20000], amount [20000], W [20000];
Scanf ("% d", & N, & vv );
For (I = 0; I <20000; I ++)
{
Bb1 [I] = inf;
BB2 [I] = inf;
}
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I]);
For (I = 0; I <n; I ++)
Scanf ("% d", & amount [I]);
Bb1 [0] = 0;
For (I = 0; I <n; I ++)
For (j = W [I]; j <= 10000; j ++)
Bb1 [J] = min (bb1 [J], bb1 [J-W [I] + 1 );
BB2 [0] = 0;
For (I = 0; I <n; I ++)
For (j = 10000; j> = W [I]; j --)
For (k = 1; k <= amount [I] & J> = K * W [I]; k ++)
BB2 [J] = min (BB2 [J], BB2 [J-W [I] * k] + k );
Ans = BB2 [VV];
For (I = vv + 1; I <= 10000; I ++)
If (ANS> BB2 [I] + bb1 [I-VV])
Ans = BB2 [I] + bb1 [I-VV];
If (ANS = inf)
Printf ("-1 \ n ");
Else
Printf ("% d \ n", ANS );
Return 0;
}
Question link: http://poj.org/problem? Id = 3260