#include <bits/stdc++.h> using namespace std; int a, b, c, A, B, C; map<pair<int, int>, int> dist; map<pair<int, int>, bool> visited; int result[100003]; int sum; inline void updateResults(queue<pair<int, int>> &q, const pair<int, int> &node, int currDist) { if (visited[node] == false) { q.push(node); visited[node] = true; } if (dist.find(node) == dist.end() || dist[node] > currDist) { dist[node] = currDist; result[node.first] = min(result[node.first], currDist); result[node.second] = min(result[node.second], currDist); result[sum - node.first - node.second] = min(result[sum - node.first - node.second], currDist); } } void bfs() { auto src = make_pair(a, b); dist[src] = 0; result[a] = 0; result[b] = 0; result[c] = 0; queue<pair<int, int>> q; q.push(src); visited[src] = true; while (!q.empty()) { auto curr = q.front(); int currDist = 1 + dist[curr]; int x = curr.first; int y = curr.second; q.pop(); // cout << "Przerabiam teraz (" << x << ", " << y << ", " << sum - x - y << ")" << endl; // x->y auto XtoY = make_pair(x+y-min(x+y, B), min(x+y, B)); if (XtoY != curr) { updateResults(q, XtoY, currDist); } // y->x auto YtoX = make_pair(min(y+x, A), x+y-min(y+x, A)); if (YtoX != curr) { updateResults(q, YtoX, currDist); } // x->z auto XtoZ = make_pair(sum-y-min(sum-y, C), y); if (XtoZ != curr) { updateResults(q, XtoZ, currDist); } // z->x auto ZtoX = make_pair(min(sum-y, A), y); if (ZtoX != curr) { updateResults(q, ZtoX, currDist); } // y->z auto YtoZ = make_pair(x, sum-x-min(sum-x, C)); if (YtoZ != curr) { updateResults(q, YtoZ, currDist); } // z->y auto ZtoY = make_pair(x, min(sum-x, B)); if (ZtoY != curr) { updateResults(q, ZtoY, currDist); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> A >> B >> C >> a >> b >> c; for (int i = 0; i <= C; ++i) { result[i] = 1<<30; } sum = a+b+c; bfs(); for (int i = 0; i < C; ++i) { cout << (result[i] == 1<<30 ? -1 : result[i]) << " "; } cout << (result[C] == 1<<30 ? -1 : result[C]); 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 | #include <bits/stdc++.h> using namespace std; int a, b, c, A, B, C; map<pair<int, int>, int> dist; map<pair<int, int>, bool> visited; int result[100003]; int sum; inline void updateResults(queue<pair<int, int>> &q, const pair<int, int> &node, int currDist) { if (visited[node] == false) { q.push(node); visited[node] = true; } if (dist.find(node) == dist.end() || dist[node] > currDist) { dist[node] = currDist; result[node.first] = min(result[node.first], currDist); result[node.second] = min(result[node.second], currDist); result[sum - node.first - node.second] = min(result[sum - node.first - node.second], currDist); } } void bfs() { auto src = make_pair(a, b); dist[src] = 0; result[a] = 0; result[b] = 0; result[c] = 0; queue<pair<int, int>> q; q.push(src); visited[src] = true; while (!q.empty()) { auto curr = q.front(); int currDist = 1 + dist[curr]; int x = curr.first; int y = curr.second; q.pop(); // cout << "Przerabiam teraz (" << x << ", " << y << ", " << sum - x - y << ")" << endl; // x->y auto XtoY = make_pair(x+y-min(x+y, B), min(x+y, B)); if (XtoY != curr) { updateResults(q, XtoY, currDist); } // y->x auto YtoX = make_pair(min(y+x, A), x+y-min(y+x, A)); if (YtoX != curr) { updateResults(q, YtoX, currDist); } // x->z auto XtoZ = make_pair(sum-y-min(sum-y, C), y); if (XtoZ != curr) { updateResults(q, XtoZ, currDist); } // z->x auto ZtoX = make_pair(min(sum-y, A), y); if (ZtoX != curr) { updateResults(q, ZtoX, currDist); } // y->z auto YtoZ = make_pair(x, sum-x-min(sum-x, C)); if (YtoZ != curr) { updateResults(q, YtoZ, currDist); } // z->y auto ZtoY = make_pair(x, min(sum-x, B)); if (ZtoY != curr) { updateResults(q, ZtoY, currDist); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> A >> B >> C >> a >> b >> c; for (int i = 0; i <= C; ++i) { result[i] = 1<<30; } sum = a+b+c; bfs(); for (int i = 0; i < C; ++i) { cout << (result[i] == 1<<30 ? -1 : result[i]) << " "; } cout << (result[C] == 1<<30 ? -1 : result[C]); return 0; } |