-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPiggy-Bank.cpp
More file actions
41 lines (39 loc) · 845 Bytes
/
Piggy-Bank.cpp
File metadata and controls
41 lines (39 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
#define Inf 0x3f3f3f3f
#define N 13666
int w[N],v[N];
int dp[N];
int main(){
int t;
cin>>t;
while(t--){
int e,f;
cin>>e>>f;
int m = f-e;
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>v[i]>>w[i];
}
for(int i=1;i<=m;i++){
dp[i] = Inf; //当未装物品时,只有dp[0]符合,其他置为正无穷或者负无穷
}
for(int i=0;i<n;i++){ //考虑前i个物品
for(int j=0;j<=m;j++){//考虑背包称重为j 的时候
if(j>=w[i]){ //如果物品能够放进去
dp[j] = min(dp[j],dp[j-w[i]]+v[i]);
}
}
}
if(dp[m]==Inf){
cout<<"This is impossible."<<endl;
}else{
cout<<"The minimum amount of money in the piggy-bank is "<<dp[m]<<"."<<endl;
}
}
return 0;
}