問題:存錢罐空重 W1、滿重 W2,容量 V = W2-W1。n 種硬幣,每種價值 v_i、重量 w_i(無限供應)。求恰好裝滿罐子的最小總價值,不可能輸出 "This is impossible."。
dp[j]:重量恰為 j 的最小價值。初始 dp[0]=0,其餘 INF。轉移:
for (i=0; i代碼
#include#include using namespace std; int f[10010], cost[510], val[510]; int main() { int T; scanf("%d", &T); while (T--) { int empty, full, n; scanf("%d%d%d", &empty, &full, &n); int V = full - empty; for (int i=0; i 經典完全背包最小值問題,注意初始化 INF 與不可達判斷。