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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <vector>
#include <unordered_set>
#include <memory>
#include <deque>
#include <array>

struct Node
{
    std::array<int, 3> data;
    int distance;
    std::shared_ptr<Node> maxNode;

    Node(int _a, int _b, int _c) : data({_a, _b, _c}), distance(0), maxNode(nullptr) {}
    Node(int _a, int _b, int _c, int dist, std::shared_ptr<Node> _maxNode) : data({_a, _b, _c}), distance(dist), maxNode(_maxNode) {}

    bool operator==(Node const &other) const
    {
        return data.at(0) == other.data.at(0) && data.at(1) == other.data.at(1) && data.at(2) == other.data.at(2);
    }

    int a() const
    {
        return data.at(0);
    }

    int b() const
    {
        return data.at(1);
    }

    int c() const
    {
        return data.at(2);
    }

    struct HashFunction
    {
        size_t operator()(Node const &node) const
        {
            size_t aHash = node.a();
            size_t bHash = node.b() + 1000000;
            size_t cHash = node.c() + 2000000;
            return aHash + bHash + cHash;
        }
    };
};

std::ostream &operator<<(std::ostream &os, Node const &node)
{
    os << "(" << node.a() << "," << node.b() << "," << node.c() << ")-" << node.distance;
    return os;
}

using NodePtr = std::shared_ptr<Node>;

Node f(Node const &node, int from, int to)
{
    if (from == to)
        std::cout << "--------------------------" << std::endl;

    int distance = node.distance + 1;
    int fromV = node.data.at(from);
    int fromM = node.maxNode->data.at(from);
    int toV = node.data.at(to);
    int toM = node.maxNode->data.at(to);
    int newTo = std::min(toM, fromV + toV);
    int newFrom = fromV - newTo + toV;

    // std::cout << fromV << "[" << fromM << "] " << toV << "[" << toM << "] " << newFrom << ":" << newTo <<std::endl;

    if (from == 0 && to == 1)
        return {newFrom, newTo, node.c(), distance, node.maxNode};
    if (from == 0 && to == 2)
        return {newFrom, node.b(),newTo, distance, node.maxNode};
    if (from == 1 && to == 0)
        return {newTo, newFrom, node.c(), distance, node.maxNode};
    if (from == 1 && to == 2)
        return {node.a(), newFrom, newTo, distance, node.maxNode};
    if (from == 2 && to == 0)
        return {newTo, node.b(), newFrom, distance, node.maxNode};

        return {node.a(), newTo, newFrom, distance, node.maxNode};
}

std::vector<int> solve(int a, int A, int b, int B, int c, int C)
{
    std::vector<int> result(C + 1, -1);
    result[a] = 0;
    result[b] = 0;
    result[c] = 0;
    std::unordered_set<Node, Node::HashFunction> nodes;
    std::unordered_set<int> founded;
    std::deque<Node> que;
    NodePtr maxNode = std::make_shared<Node>(A, B, C);
    int expectedFounded = std::min(C+1,a+b+c+1);
    nodes.emplace(a, b, c, 0, maxNode);
    founded.insert({a,b,c});
    que.emplace_back(a, b, c, 0, maxNode);

    auto const update = [&](Node const& node) -> void
    {
        if (nodes.count(node) == 0)
        {
            que.push_back(node);
            nodes.insert(node);
            for (int i =0;i<3;++i)
            {
                if (result[node.data[i]] == -1)
                {
                    result[node.data[i]] = node.distance;
                }
                else
                {
                    result[node.data[i]] = std::min(node.distance, result[node.data[i]]);
                }
                founded.insert(node.data[i]);
            }
        }
    };
    while(que.empty() == false)
    {
        Node tmp = que.front();
        update(f(tmp, 0, 1));
        update(f(tmp, 0, 2));
        update(f(tmp, 1, 0));
        update(f(tmp, 1, 2));
        update(f(tmp, 2, 0));
        update(f(tmp, 2, 1));
        // std::cout << tmp << std::endl;
        if (founded.size() == expectedFounded)
            break;
        que.pop_front();
    }

    // for (auto const& i : que)
    //     std::cout << i << std::endl;
    return result;
}

int main()
{
    int A, B, C;
    int a, b, c;

    std::cin >> A >> B >> C;
    std::cin >> a >> b >> c;

    auto const &result = solve(a, A, b, B, c, C);

    for (auto const &i : result)
        std::cout << i << " ";
    return 0;
}