#include <bits/stdc++.h> using namespace std; typedef long long lld; typedef tuple<int, int, int> T; #define ff first #define ss second #define mp make_pair #define mt make_tuple constexpr int N = 1 << 17; constexpr int inf = 1 << 30; int v[4]; int res[N]; map<T, bool> dp; queue<pair<T, int>> q; int c[3]; void pack_and_push(T p, int k) { if (dp[p]) return; dp[p] = true; res[get<0>(p)] = min(res[get<0>(p)], k); res[get<1>(p)] = min(res[get<1>(p)], k); res[get<2>(p)] = min(res[get<2>(p)], k); q.push(mp(p, k)); } void consider_transfers(T p, int k) { for (int i = 0; i < 3; ++i) { for (int j = (i + 1) % 3; j != i; j = (j + 1) % 3) { // przelewamy z i-tego do j-tego tie(c[0], c[1], c[2]) = p; int d = min(c[i], v[j] - c[j]); c[i] -= d; c[j] += d; pack_and_push(mt(c[0], c[1], c[2]), k + 1); } } } int main(){ scanf("%d%d%d", v, v + 1, v + 2); scanf("%d%d%d", c, c + 1, c + 2); for (int i = 0; i <= v[2]; ++i) { res[i] = inf; } res[c[0]] = 0; res[c[1]] = 0; res[c[2]] = 0; q.push(mp(mt(c[0], c[1], c[2]), 0)); while (!q.empty()) { auto p = q.front(); q.pop(); consider_transfers(p.ff, p.ss); } for (int i = 0; i <= v[2]; ++i) { printf("%d ", res[i] == inf ? -1 : res[i]); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long lld; typedef tuple<int, int, int> T; #define ff first #define ss second #define mp make_pair #define mt make_tuple constexpr int N = 1 << 17; constexpr int inf = 1 << 30; int v[4]; int res[N]; map<T, bool> dp; queue<pair<T, int>> q; int c[3]; void pack_and_push(T p, int k) { if (dp[p]) return; dp[p] = true; res[get<0>(p)] = min(res[get<0>(p)], k); res[get<1>(p)] = min(res[get<1>(p)], k); res[get<2>(p)] = min(res[get<2>(p)], k); q.push(mp(p, k)); } void consider_transfers(T p, int k) { for (int i = 0; i < 3; ++i) { for (int j = (i + 1) % 3; j != i; j = (j + 1) % 3) { // przelewamy z i-tego do j-tego tie(c[0], c[1], c[2]) = p; int d = min(c[i], v[j] - c[j]); c[i] -= d; c[j] += d; pack_and_push(mt(c[0], c[1], c[2]), k + 1); } } } int main(){ scanf("%d%d%d", v, v + 1, v + 2); scanf("%d%d%d", c, c + 1, c + 2); for (int i = 0; i <= v[2]; ++i) { res[i] = inf; } res[c[0]] = 0; res[c[1]] = 0; res[c[2]] = 0; q.push(mp(mt(c[0], c[1], c[2]), 0)); while (!q.empty()) { auto p = q.front(); q.pop(); consider_transfers(p.ff, p.ss); } for (int i = 0; i <= v[2]; ++i) { printf("%d ", res[i] == inf ? -1 : res[i]); } return 0; } |