// C5 - Butelki #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <unordered_map> using namespace std; int pour(long long from, long long to, long long toCap) { return min(toCap, from + to) - to; } long long codeKey(long long a, long long b) { return a * 100001 + b; } void uncodeKey(long long val, long long &a, long long &b) { a = val / 100001; b = val % 100001; } int main() { long long a, b, c, x, y, z, d, e; long long sum; unordered_map<long long, long long> configs; queue<long long> candidates; vector<long long> results; cin >> x; cin >> y; cin >> z; cin >> a; cin >> b; cin >> c; sum = a + b + c; for (int i = 0; i <= z; ++i) { results.push_back(-1); } configs[codeKey(a, b)] = 0; candidates.push(codeKey(a, b)); while (candidates.empty() == false) { uncodeKey(candidates.front(), a, b); candidates.pop(); c = sum - a - b; if (results[a] < 0) { results[a] = configs[codeKey(a, b)]; } if (results[b] < 0) { results[b] = configs[codeKey(a, b)]; } if (results[c] < 0) { results[c] = configs[codeKey(a, b)]; } if (a > 0 && b < y) { d = a - pour(a, b, y); e = b + pour(a, b, y); // f = c; if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (a > 0 && c < z) { d = a - pour(a, c, z); e = b; // f = c + pour(a, c, z); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (b > 0 && a < x) { d = a + pour(b, a, x); e = b - pour(b, a, x); // f = c; if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (b > 0 && c < z) { d = a; e = b - pour(b, c, z); // f = c + pour(b, c, z); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (c > 0 && a < x) { d = a + pour(c, a, x); e = b; // f = c - pour(c, a, x); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (c > 0 && b < y) { d = a; e = b + pour(c, b, y); // f = c - pour(c, b, y); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } } for (int i = 0; i <= z; ++i) { cout << results[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 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 | // C5 - Butelki #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <unordered_map> using namespace std; int pour(long long from, long long to, long long toCap) { return min(toCap, from + to) - to; } long long codeKey(long long a, long long b) { return a * 100001 + b; } void uncodeKey(long long val, long long &a, long long &b) { a = val / 100001; b = val % 100001; } int main() { long long a, b, c, x, y, z, d, e; long long sum; unordered_map<long long, long long> configs; queue<long long> candidates; vector<long long> results; cin >> x; cin >> y; cin >> z; cin >> a; cin >> b; cin >> c; sum = a + b + c; for (int i = 0; i <= z; ++i) { results.push_back(-1); } configs[codeKey(a, b)] = 0; candidates.push(codeKey(a, b)); while (candidates.empty() == false) { uncodeKey(candidates.front(), a, b); candidates.pop(); c = sum - a - b; if (results[a] < 0) { results[a] = configs[codeKey(a, b)]; } if (results[b] < 0) { results[b] = configs[codeKey(a, b)]; } if (results[c] < 0) { results[c] = configs[codeKey(a, b)]; } if (a > 0 && b < y) { d = a - pour(a, b, y); e = b + pour(a, b, y); // f = c; if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (a > 0 && c < z) { d = a - pour(a, c, z); e = b; // f = c + pour(a, c, z); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (b > 0 && a < x) { d = a + pour(b, a, x); e = b - pour(b, a, x); // f = c; if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (b > 0 && c < z) { d = a; e = b - pour(b, c, z); // f = c + pour(b, c, z); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (c > 0 && a < x) { d = a + pour(c, a, x); e = b; // f = c - pour(c, a, x); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } if (c > 0 && b < y) { d = a; e = b + pour(c, b, y); // f = c - pour(c, b, y); if (configs.find(codeKey(d, e)) == configs.end()) { configs[codeKey(d, e)] = configs[codeKey(a, b)] + 1; candidates.push(codeKey(d, e)); } } } for (int i = 0; i <= z; ++i) { cout << results[i] << " "; } return 0; } |