#include <bits/stdc++.h> using namespace std; const int N = 100'007; const int INF = 1'000'000'007; int A, B, C; int limit[3]; int ans[N]; queue <tuple <int, int, int> > Q; map <tuple <int, int, int>, int > D; void get_edges(vector <int> vec, int d) { auto update = [&](int a, int b, int c) { if (!D.count({a, b, c})) { D[{a, b, c}] = d + 1; Q.push({a, b, c}); } }; for (int l = 0; l < 3; ++l) { for (int r = 0; r < 3; ++r) { if (l == r) { continue; } int clr = min(vec[l], limit[r] - vec[r]); if (clr == 0) { continue; } vector <int> next_vec = vec; next_vec[l] -= clr; next_vec[r] += clr; update(next_vec[0], next_vec[1], next_vec[2]); } } } int main() { cin >> A >> B >> C; limit[0] = A, limit[1] = B, limit[2] = C; for (int i = 0; i <= C; ++i) { ans[i] = INF; } int sa, sb, sc; cin >> sa >> sb >> sc; D[{sa, sb, sc}] = 0; Q.push({sa, sb, sc}); while (!Q.empty()) { const auto [a, b, c] = Q.front(); Q.pop(); const int &d = D[{a, b, c}]; ans[a] = min(ans[a], d); ans[b] = min(ans[b], d); ans[c] = min(ans[c], d); get_edges({a, b, c}, d); } for (int i = 0; i <= C; ++i) { if (ans[i] == INF) { ans[i] = -1; } printf("%d%c", ans[i], " \n"[i == C]); } }
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 | #include <bits/stdc++.h> using namespace std; const int N = 100'007; const int INF = 1'000'000'007; int A, B, C; int limit[3]; int ans[N]; queue <tuple <int, int, int> > Q; map <tuple <int, int, int>, int > D; void get_edges(vector <int> vec, int d) { auto update = [&](int a, int b, int c) { if (!D.count({a, b, c})) { D[{a, b, c}] = d + 1; Q.push({a, b, c}); } }; for (int l = 0; l < 3; ++l) { for (int r = 0; r < 3; ++r) { if (l == r) { continue; } int clr = min(vec[l], limit[r] - vec[r]); if (clr == 0) { continue; } vector <int> next_vec = vec; next_vec[l] -= clr; next_vec[r] += clr; update(next_vec[0], next_vec[1], next_vec[2]); } } } int main() { cin >> A >> B >> C; limit[0] = A, limit[1] = B, limit[2] = C; for (int i = 0; i <= C; ++i) { ans[i] = INF; } int sa, sb, sc; cin >> sa >> sb >> sc; D[{sa, sb, sc}] = 0; Q.push({sa, sb, sc}); while (!Q.empty()) { const auto [a, b, c] = Q.front(); Q.pop(); const int &d = D[{a, b, c}]; ans[a] = min(ans[a], d); ans[b] = min(ans[b], d); ans[c] = min(ans[c], d); get_edges({a, b, c}, d); } for (int i = 0; i <= C; ++i) { if (ans[i] == INF) { ans[i] = -1; } printf("%d%c", ans[i], " \n"[i == C]); } } |