aboutsummaryrefslogtreecommitdiff
path: root/03_dynamic_programming/minimizing_coins_1634.cpp
diff options
context:
space:
mode:
Diffstat (limited to '03_dynamic_programming/minimizing_coins_1634.cpp')
-rw-r--r--03_dynamic_programming/minimizing_coins_1634.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/03_dynamic_programming/minimizing_coins_1634.cpp b/03_dynamic_programming/minimizing_coins_1634.cpp
new file mode 100644
index 0000000..f6189da
--- /dev/null
+++ b/03_dynamic_programming/minimizing_coins_1634.cpp
@@ -0,0 +1,33 @@
1#include <algorithm>
2#include <iostream>
3#include <queue>
4#include <vector>
5
6int f(const std::vector<int>& c, int x) {
7 static constexpr int max = 999999999;
8 std::vector<int> a(x+1, max);
9 std::queue<int> q;
10 a[0] = 0;
11 q.push(0);
12 while (!q.empty()) {
13 auto i = q.front();
14 q.pop();
15 for (auto k : c) {
16 if (i + k > x || a[i+k] <= a[i]+1) continue;
17 if (i + k == x) return a[i] + 1;
18 a[i+k] = a[i] + 1;
19 q.push(i + k);
20 }
21 }
22 return -1;
23}
24
25int main() {
26 int n, x;
27 std::cin >> n >> x;
28 std::vector<int> c(n);
29 for (int i = 0; i < n; i++)
30 std::cin >> c[i];
31
32 std::cout << f(c, x) << "\n";
33}

Generated with cgit - Back to sebastiano.tronto.net