-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECoins_UVA10306OJ.cpp
69 lines (56 loc) · 1.45 KB
/
ECoins_UVA10306OJ.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct coin
{
int x;
int y;
};
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCase; cin >> testCase;
for (int t = 1; t <= testCase; ++t)
{
int m, s; cin >> m >> s;
vector<coin> a(m);
for (int i = 0; i <= m - 1; ++i)
{
cin >> a[i].x >> a[i].y;
}
vector<vector<int>> dp(s + 1, vector<int>(s + 1, INT_MAX));
int ans = INT_MAX;
dp[0][0] = 0;
for (int i = 0; i <= s; ++i)
{
for (int j = 0; j <= s; ++j)
{
for (int k = 0; k <= m - 1; ++k)
{
if (i - a[k].x < 0 || j - a[k].y < 0)
{
continue;
}
if (dp[i - a[k].x][j - a[k].y] < INT_MAX)
{
dp[i][j] = min(dp[i][j], dp[i - a[k].x][j - a[k].y] + 1);
}
if (i * i + j * j == s * s && dp[i][j] < INT_MAX)
{
ans = min(ans, dp[i][j]);
}
}
}
}
if (ans == INT_MAX)
{
cout << "not possible\n";
}
else
{
cout << ans << '\n';
}
}
cout.flush();
return 0;
}