#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif // #define MULTI_TASK true const int INF = 1000 * 1000 * 1000; const LL INFLL = 1e18; const LL MAKSN = 1000 * 1000 + 13; const LL MUL = (LL)1e7; const LL MOD = 123456789LL; LL N, M, k, l, A, B, C, a, b, c, d; vector<LL> v, caps; vector<LL> ans; set<LL> vis; map<LL, int> dist; int found = 0; void zeruj() { v.clear(); } void readIn() { cin >> A >> B >> C >> a >> b >> c; caps = {A, B, C}; ans.resize(C + 1, -1); } LL calcMask(LL a, LL b, LL c) { return (a * MUL * MUL) + b * MUL + c; } void updateAns(LL a, LL b, LL c, int dist) { if (ans[a] == -1) { ans[a] = dist; found++; } if (ans[b] == -1) { ans[b] = dist; found++; } if (ans[c] == -1) { ans[c] = dist; found++; } } void solve() { queue<tuple<int, int, int>> q; q.push({a, b, c}); updateAns(a, b, c, 0); LL mask = calcMask(a, b, c); dist[mask] = 0; vis.insert(mask); while (found <= C + 1 && !q.empty()) { tuple<int, int, int> f = q.front(); q.pop(); vector<int> v = {get<0>(f), get<1>(f), get<2>(f)}; LL mask = calcMask(v[0], v[1], v[2]); int d = dist[mask]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; // Z i do j przelewamy vector<int> vcp = v; int ile_wejdzie = caps[j] - vcp[j]; int ile_przelejemy = min(ile_wejdzie, vcp[i]); vcp[j] += ile_przelejemy; vcp[i] -= ile_przelejemy; LL mask2 = calcMask(vcp[0], vcp[1], vcp[2]); if (vis.find(mask2) == vis.end()) { dist[mask2] = d + 1; vis.insert(mask2); q.push({vcp[0], vcp[1], vcp[2]}); updateAns(vcp[0], vcp[1], vcp[2], d + 1); } } } } for (auto a : ans) { cout << a << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); #ifdef MULTI_TASK int test; cin >> test; while (test--) { #endif zeruj(); readIn(); solve(); #ifdef MULTI_TASK } #endif return 0; }
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif // #define MULTI_TASK true const int INF = 1000 * 1000 * 1000; const LL INFLL = 1e18; const LL MAKSN = 1000 * 1000 + 13; const LL MUL = (LL)1e7; const LL MOD = 123456789LL; LL N, M, k, l, A, B, C, a, b, c, d; vector<LL> v, caps; vector<LL> ans; set<LL> vis; map<LL, int> dist; int found = 0; void zeruj() { v.clear(); } void readIn() { cin >> A >> B >> C >> a >> b >> c; caps = {A, B, C}; ans.resize(C + 1, -1); } LL calcMask(LL a, LL b, LL c) { return (a * MUL * MUL) + b * MUL + c; } void updateAns(LL a, LL b, LL c, int dist) { if (ans[a] == -1) { ans[a] = dist; found++; } if (ans[b] == -1) { ans[b] = dist; found++; } if (ans[c] == -1) { ans[c] = dist; found++; } } void solve() { queue<tuple<int, int, int>> q; q.push({a, b, c}); updateAns(a, b, c, 0); LL mask = calcMask(a, b, c); dist[mask] = 0; vis.insert(mask); while (found <= C + 1 && !q.empty()) { tuple<int, int, int> f = q.front(); q.pop(); vector<int> v = {get<0>(f), get<1>(f), get<2>(f)}; LL mask = calcMask(v[0], v[1], v[2]); int d = dist[mask]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; // Z i do j przelewamy vector<int> vcp = v; int ile_wejdzie = caps[j] - vcp[j]; int ile_przelejemy = min(ile_wejdzie, vcp[i]); vcp[j] += ile_przelejemy; vcp[i] -= ile_przelejemy; LL mask2 = calcMask(vcp[0], vcp[1], vcp[2]); if (vis.find(mask2) == vis.end()) { dist[mask2] = d + 1; vis.insert(mask2); q.push({vcp[0], vcp[1], vcp[2]}); updateAns(vcp[0], vcp[1], vcp[2], d + 1); } } } } for (auto a : ans) { cout << a << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); #ifdef MULTI_TASK int test; cin >> test; while (test--) { #endif zeruj(); readIn(); solve(); #ifdef MULTI_TASK } #endif return 0; } |