// Example program #include <iostream> #include <string> #include <vector> #include <queue> #include <tuple> #include <unordered_map> #include <map> #define MAXN 100003 using namespace std; int CAP[3], init_a, init_b, init_c; struct State { int val[3]; }; bool operator<(const State &f, const State &s) { if (f.val[0] < s.val[0]) { return true; } else if (f.val[0] == s.val[0]) { if(f.val[1] < s.val[1]) { return true; } else if(f.val[1] == s.val[1]) { if(f.val[2] < s.val[2]) { return true; } } } return false; } queue<State> Q; //unordered_map<tuple<int, int, int>, long long> min_dist; map<State, long long> min_dist; int f, t; long long cur_dist; State new_s, s; long long inf = -1; long long res[MAXN]; int n_of_res; State make_state(int v1, int v2, int v3) { State s; s.val[0] = v1; s.val[1] = v2; s.val[2] = v3; return s; } State make_state2(State s, int from_index, int to_index) { int f = s.val[from_index]; int t = s.val[to_index]; State new_s; new_s.val[0] = s.val[0]; new_s.val[1] = s.val[1]; new_s.val[2] = s.val[2]; if (f + t > CAP[to_index]) { new_s.val[to_index] = CAP[to_index]; new_s.val[from_index] = (f + t) - CAP[to_index]; } else { new_s.val[from_index] = 0; new_s.val[to_index] = (f + t); } return new_s; } int main() { scanf("%d %d %d", &CAP[0], &CAP[1], &CAP[2]); scanf("%d %d %d", &init_a, &init_b, &init_c); for (int i = 0; i <= CAP[2]; i++) { res[i] = inf; } min_dist[make_state(init_a, init_b, init_c)] = 0; Q.push(make_state(init_a, init_b, init_c)); res[init_a] = 0; res[init_b] = 0; res[init_c] = 0; n_of_res = 1; if (init_b != init_a) { n_of_res++; } if (init_c != init_b && init_c != init_a) { n_of_res++; } int from_index[6] = {0, 0, 1, 1, 2, 2}; int to_index[6] = {1, 2, 0, 2, 0, 1}; while(!Q.empty() && (n_of_res < CAP[2] + 1)) { s = Q.front(); Q.pop(); cur_dist = min_dist[s]; for (int i = 0; i < 6; i++) { new_s = make_state2(s, from_index[i], to_index[i]); if (min_dist.find(new_s) == min_dist.end()) { min_dist[new_s] = cur_dist + 1; for (int j = 0; j < 3; j++) { if (res[new_s.val[j]] == inf) { res[new_s.val[j]] = cur_dist + 1; n_of_res++; } } Q.push(new_s); } } } for (int i = 0; i < CAP[2]; i++) { printf("%lld ", res[i]); } printf("%lld\n", res[CAP[2]]); }
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 | // Example program #include <iostream> #include <string> #include <vector> #include <queue> #include <tuple> #include <unordered_map> #include <map> #define MAXN 100003 using namespace std; int CAP[3], init_a, init_b, init_c; struct State { int val[3]; }; bool operator<(const State &f, const State &s) { if (f.val[0] < s.val[0]) { return true; } else if (f.val[0] == s.val[0]) { if(f.val[1] < s.val[1]) { return true; } else if(f.val[1] == s.val[1]) { if(f.val[2] < s.val[2]) { return true; } } } return false; } queue<State> Q; //unordered_map<tuple<int, int, int>, long long> min_dist; map<State, long long> min_dist; int f, t; long long cur_dist; State new_s, s; long long inf = -1; long long res[MAXN]; int n_of_res; State make_state(int v1, int v2, int v3) { State s; s.val[0] = v1; s.val[1] = v2; s.val[2] = v3; return s; } State make_state2(State s, int from_index, int to_index) { int f = s.val[from_index]; int t = s.val[to_index]; State new_s; new_s.val[0] = s.val[0]; new_s.val[1] = s.val[1]; new_s.val[2] = s.val[2]; if (f + t > CAP[to_index]) { new_s.val[to_index] = CAP[to_index]; new_s.val[from_index] = (f + t) - CAP[to_index]; } else { new_s.val[from_index] = 0; new_s.val[to_index] = (f + t); } return new_s; } int main() { scanf("%d %d %d", &CAP[0], &CAP[1], &CAP[2]); scanf("%d %d %d", &init_a, &init_b, &init_c); for (int i = 0; i <= CAP[2]; i++) { res[i] = inf; } min_dist[make_state(init_a, init_b, init_c)] = 0; Q.push(make_state(init_a, init_b, init_c)); res[init_a] = 0; res[init_b] = 0; res[init_c] = 0; n_of_res = 1; if (init_b != init_a) { n_of_res++; } if (init_c != init_b && init_c != init_a) { n_of_res++; } int from_index[6] = {0, 0, 1, 1, 2, 2}; int to_index[6] = {1, 2, 0, 2, 0, 1}; while(!Q.empty() && (n_of_res < CAP[2] + 1)) { s = Q.front(); Q.pop(); cur_dist = min_dist[s]; for (int i = 0; i < 6; i++) { new_s = make_state2(s, from_index[i], to_index[i]); if (min_dist.find(new_s) == min_dist.end()) { min_dist[new_s] = cur_dist + 1; for (int j = 0; j < 3; j++) { if (res[new_s.val[j]] == inf) { res[new_s.val[j]] = cur_dist + 1; n_of_res++; } } Q.push(new_s); } } } for (int i = 0; i < CAP[2]; i++) { printf("%lld ", res[i]); } printf("%lld\n", res[CAP[2]]); } |