#include <bits/stdc++.h> namespace std{ namespace { template <class T> inline void hash_combine(std::size_t& seed, T const& v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl<Tuple, Index-1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple,0> { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; } template <typename ... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const& tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...> >::apply(seed, tt); return seed; } }; } using namespace std; typedef long long LL; typedef tuple<int, int, int> T; const int MAX = 1e5+5; int res[MAX]; const int INF = MAX+1; int main() { ios_base::sync_with_stdio(false); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; for(int i = 0; i <= C; ++i) res[i] = INF; unordered_set<T> s; deque<pair<int, T>> q; q.push_back(make_pair(0, make_tuple(a, b, c))); while(!q.empty()) { pair<int, T> t = q.front(); q.pop_front(); int a = get<0>(t.second); int b = get<1>(t.second); int c = get<2>(t.second); res[a] = min(res[a], t.first); res[b] = min(res[b], t.first); res[c] = min(res[c], t.first); T nt[] = { //1->2 make_tuple(max(0, a-(B-b)), min(a+b, B), c), //1->3 make_tuple(max(0, a-(C-c)), b, min(a+c, C)), //2->1 make_tuple(min(b+a, A), max(0, b-(A-a)), c), //2->3 make_tuple(a, max(0, b-(C-c)), min(b+c, C)), //3->1 make_tuple(min(c+a, A), b, max(0, c-(A-a))), //3->2 make_tuple(a, min(b+c, B), max(0, c-(B-b))), }; for(int i = 0; i < 6; ++i) { if(s.count(nt[i])) continue; s.insert(nt[i]); q.push_back(make_pair(t.first+1, nt[i])); } } for(int i = 0; i <= C; ++i) { if(res[i] == INF) cout << -1 << ' '; else cout << res[i] << ' '; } cout << '\n'; }
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 | #include <bits/stdc++.h> namespace std{ namespace { template <class T> inline void hash_combine(std::size_t& seed, T const& v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl<Tuple, Index-1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple,0> { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; } template <typename ... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const& tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...> >::apply(seed, tt); return seed; } }; } using namespace std; typedef long long LL; typedef tuple<int, int, int> T; const int MAX = 1e5+5; int res[MAX]; const int INF = MAX+1; int main() { ios_base::sync_with_stdio(false); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; for(int i = 0; i <= C; ++i) res[i] = INF; unordered_set<T> s; deque<pair<int, T>> q; q.push_back(make_pair(0, make_tuple(a, b, c))); while(!q.empty()) { pair<int, T> t = q.front(); q.pop_front(); int a = get<0>(t.second); int b = get<1>(t.second); int c = get<2>(t.second); res[a] = min(res[a], t.first); res[b] = min(res[b], t.first); res[c] = min(res[c], t.first); T nt[] = { //1->2 make_tuple(max(0, a-(B-b)), min(a+b, B), c), //1->3 make_tuple(max(0, a-(C-c)), b, min(a+c, C)), //2->1 make_tuple(min(b+a, A), max(0, b-(A-a)), c), //2->3 make_tuple(a, max(0, b-(C-c)), min(b+c, C)), //3->1 make_tuple(min(c+a, A), b, max(0, c-(A-a))), //3->2 make_tuple(a, min(b+c, B), max(0, c-(B-b))), }; for(int i = 0; i < 6; ++i) { if(s.count(nt[i])) continue; s.insert(nt[i]); q.push_back(make_pair(t.first+1, nt[i])); } } for(int i = 0; i <= C; ++i) { if(res[i] == INF) cout << -1 << ' '; else cout << res[i] << ' '; } cout << '\n'; } |