标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14395 Accepted Submission(s): 7313
#include <cstdio> #include <iostream> #include <queue> #include <cstring> #include <algorithm> using namespace std ; int n, m, E, F ; int dp[10500] ; int v[550], w[550] ; int getans(int sx) { int& res = dp[sx] ; if(res != -1) return res ; res = 1 << 25 ; for(int i = 1; i <= n; ++i) if(sx >= w[i]) res = min(res, getans(sx - w[i]) + v[i]) ; return res ; } int main() { int _ ; scanf("%d",&_) ; while(_--) { memset(dp, -1, sizeof dp) ; scanf("%d%d%d",&E,&F,&n) ; for(int i = 1; i <= n; ++i) scanf("%d%d",&v[i],&w[i]) ; int s = F - E ; dp[0] = 0 ; int ans = getans(s) ; if(ans == 1 << 25) printf("This is impossible.\n") ; else printf("The minimum amount of money in the piggy-bank is %d.\n",ans) ; } }
#include <cstdio> #include <iostream> #include <queue> #include <cstring> #include <algorithm> using namespace std ; int dp[10500] ; int v[550], w[550] ; int main() { int _ ; scanf("%d",&_) ; while(_--) { int n, E, F ; scanf("%d%d%d",&E,&F,&n) ; int s = F - E ; for(int i = 1; i <= n ;++i) scanf("%d%d",&v[i],&w[i]) ; for(int i = 0; i <= 10005; ++i) dp[i] = 1 << 25 ; dp[0] = 0 ; for(int i = 1; i <= n; ++i) for(int j = w[i]; j <= s; ++j) dp[j] = min(dp[j],dp[j - w[i]] + v[i]) ; if(dp[s] == 1 << 25) printf("This is impossible.\n") ; else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[s]) ; } }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4507207.html