题目1454:Piggy-Bank(完全背包问题)
题目链接:http://ac.jobdu.com/problem.php?pid=1454
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1454 Piggy-Bank.cpp // Jobdu // // Created by PengFei_Zheng on 25/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #include <limits.h> #define MAX_SIZE 10001 #define MAX_NUM 501 using namespace std; struct Coin{ int value; int space; }; int t, e, f, n; int dp[MAX_SIZE]; Coin coin[MAX_NUM]; int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&e,&f); scanf("%d",&n); int space = f - e; for(int i = 1 ; i <= n ; i++){ scanf("%d%d",&coin[i].value,&coin[i].space); } for(int i = 0 ; i <= space ; i++){ i == 0 ? dp[i] = 0 : dp[i] = INT_MAX; } for(int i = 1 ; i <= n ; i++){ for(int j = coin[i].space ; j <= space ; j++){ if(dp[j-coin[i].space] != INT_MAX) dp[j] = min(dp[j], dp[j-coin[i].space]+coin[i].value); } } if(dp[space] != INT_MAX) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[space]); else printf("This is impossible.\n"); } return 0; } /************************************************************** Problem: 1454 User: zpfbuaa Language: C++ Result: Accepted Time:50 ms Memory:1560 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.