forked from chr4ss1/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIGBANK.cpp
More file actions
79 lines (57 loc) · 1.38 KB
/
PIGBANK.cpp
File metadata and controls
79 lines (57 loc) · 1.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <utility>
#include <queue>
#include <vector>
#include <map>
using namespace std;
int main()
{
int testCases;
scanf("%d", &testCases);
while(testCases--)
{
int weightOfEmptyPig;
int weightOfRequired;
int coins;
scanf("%d %d", &weightOfEmptyPig, &weightOfRequired);
scanf("%d", &coins);
vector<pair<int, int> > coinList;
for(int j = 0; j < coins; j++)
{
int value, weight;
scanf("%d %d", &value, &weight);
coinList.push_back(make_pair(value, weight));
}
if(weightOfRequired == weightOfEmptyPig)
{
printf("This is impossible.\r\n");
continue;
}
int ck = weightOfRequired - weightOfEmptyPig+1;
int *xd = new int[ck];
for(int i = 0; i < ck; i++)
xd[i] = -1;
xd[0] = 0;
for(int weightOffset = 1; weightOffset < ck; weightOffset++)
{
int useless = true;
for(int coin = 0; coin < coinList.size(); coin++)
{
int coinWeight = coinList[coin].second;
int coinValue = coinList[coin].first;
if(weightOffset - coinWeight < 0) continue;
int rx = xd[weightOffset - coinWeight];
if(rx == -1) continue;
rx += coinValue;
if(rx < xd[weightOffset] || xd[weightOffset] == -1)
xd[weightOffset] = rx;
}
}
int a = xd[ck - 1];
if(a == -1)
printf("This is impossible.\r\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\r\n", a);
}
return 0;
}