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

typedef array<int, 3> Triple;

int main() {
   //freopen("1.in", "r", stdin);
   ios_base::sync_with_stdio(0);
   cin.tie(0);

   Triple ABC;
   Triple abc;

   for (auto& c : ABC) {
      cin >> c;
   }

   for (auto& c : abc) {
      cin >> c;
   }

   vector<int> result(ABC[2] + 1, -1);

   set<Triple> skip;
   priority_queue<pair<int, Triple >> pq;

   pq.push({0, abc});
   skip.insert(abc);

   while (!pq.empty()) {
      int d;
      tie(d, abc) = pq.top();
      pq.pop();
      d = -d;

      for (int x : abc) {
         if (result[x] == -1 || result[x] > d) {
            result[x] = d;
         }
      }
      for (int s = 0; s < 3; s++) {
         for (int t = 0; t < 3; t++) {
            if (s == t)
               continue;
            if (abc[s] == 0 || abc[t] == ABC[t]) // <=> delta == 0
               continue;

            int delta = min(abc[s], ABC[t] - abc[t]);
            Triple abc_ = abc;
            abc_[s] -= delta;
            abc_[t] += delta;
            if (skip.count(abc_) == 0) { // and not in pq?
               skip.insert(abc_);
               pq.push({-(d + 1), abc_});
            }
         }
      }
   }

   for (int r : result) {
      cout << r << " ";
   }
}