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
#include <bits/stdc++.h>
using namespace std;

#define boost                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)

typedef tuple<int, int, int> state;

int main() {
    boost;

    int A, B, C, a, b, c;
    cin >> A >> B >> C >> a >> b >> c;

    vector<int> cap({A, B, C});

    vector<int> res(C+1, INT_MAX);

    map<state, int> dist;
    state start = {a, b, c};
    dist[start] = 0;
    queue<state> q;
    q.push(start);

    while(!q.empty())
    {
        state s = q.front();
        q.pop();
        int our_dist = dist[s];

        vector<int> v({get<0>(s), get<1>(s), get<2>(s)});
        for (int x : v)
            res[x] = min(res[x], our_dist);

        for (int from = 0; from < 3; ++from) {
            for (int to = 0; to < 3; ++to) {
                if (to != from) {
                    vector<int> w(v);
                    int amount_to_transfer = min(cap[to] - v[to], v[from]);
                    w[from] -= amount_to_transfer;
                    w[to] += amount_to_transfer;

                    state new_state = {w[0], w[1], w[2]};
                    if (dist.find(new_state) == dist.end()) {
                        dist[new_state] = our_dist + 1;
                        q.push(new_state);
                    }
                }
            }
        }
    }

    for (int i = 0; i <= C; ++i)
        cout << (res[i] == INT_MAX ? -1 : res[i]) << ' ';

    return 0;
}