标签:
Description
Input
Output
Sample Input
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 int T,E,F,V,n; 9 const int N=1E4+5; 10 int dp[N],w[N],p[N]; 11 const int inf=99999999; 12 13 void solve (int cost,int value) 14 { 15 for ( int i = cost ; i <= V ; i++) 16 if (dp[i]>0) 17 dp[i] = min(dp[i],dp[i-cost]+value); 18 } 19 20 void init() 21 { 22 for ( int i = 0 ; i <= N ; i++) 23 dp[i] = inf ; 24 dp[0]=0; 25 } 26 27 int main() 28 { 29 scanf("%d",&T); 30 while ( T--) 31 { 32 init(); 33 scanf("%d %d",&E,&F); 34 V =F - E; 35 scanf("%d",&n); 36 for ( int i = 1 ; i <= n ; i++ ) 37 scanf("%d %d",&p[i],&w[i]); 38 for ( int i = 1 ; i <= n ; i++ ) 39 solve(w[i],p[i]); 40 // for ( int i = 1 ; i <= V ; i++) 41 // if (dp[i]>0) 42 // cout<<"dp[i]:"<<dp[i]<<endl; 43 44 if (dp[V]==inf) 45 { 46 printf("This is impossible.\n"); 47 } 48 else 49 { 50 printf("The minimum amount of money in the piggy-bank is %d.\n",dp[V]); 51 } 52 53 } 54 return 0; 55 }
Sample Output
标签:
原文地址:http://www.cnblogs.com/111qqz/p/4395024.html