#include <vector> #include <iostream> #include <set> #include <map> #include <string> #include <list> using namespace std; #define MAXN 100010 #define NONE 2147483647 int a_cap, b_cap, c_cap; struct node { int a, b, c; bool operator < (const node &other) const { if (a == other.a) { if (b == other.b) { return c < other.c; } else { return b < other.b; } } else { return a < other.a; } } }; struct node_with_dist { int a, b, c, dist; }; set<node> visited_nodes; list<node_with_dist> queue; int missing_scores; int results[MAXN]; void update_scores(int a, int b, int c, int dist) { if (results[a] > dist) { //printf("Set first result for %d = %d\n", a, dist); results[a] = dist; missing_scores--; } if (results[b] > dist) { //printf("Set first result for %d = %d\n", b, dist); results[b] = dist; missing_scores--; } if (results[c] > dist) { //printf("Set first result for %d = %d\n", c, dist); results[c] = dist; missing_scores--; } } void queue_new(node_with_dist* nwd) { node n = node(); n.a = nwd->a; n.b = nwd->b; n.c = nwd->c; if (visited_nodes.find(n) == visited_nodes.end()) { queue.push_back(*nwd); } } void check_possibilities(int a, int b, int c, int dist) { // a --> b if (a > 0 && b < b_cap) { int diff = min(a, b_cap - b); node_with_dist m = node_with_dist(); m.a = a - diff; m.b = b + diff; m.c = c; m.dist = dist+1; //printf("CAN GO a->b (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // a --> c if (a > 0 && c < c_cap) { int diff = min(a, c_cap - c); node_with_dist m = node_with_dist(); m.a = a - diff; m.b = b; m.c = c + diff; m.dist = dist+1; //printf("CAN GO a->c (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // b --> a if (b > 0 && a < a_cap) { int diff = min(b, a_cap - a); node_with_dist m = node_with_dist(); m.a = a + diff; m.b = b - diff; m.c = c; m.dist = dist+1; //printf("CAN GO b->a (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // b --> c if (b > 0 && c < c_cap) { int diff = min(b, c_cap - c); node_with_dist m = node_with_dist(); m.a = a; m.b = b - diff; m.c = c + diff; m.dist = dist+1; //printf("CAN GO b->c (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // c --> a if (c > 0 && a < a_cap) { int diff = min(c, a_cap - a); node_with_dist m = node_with_dist(); m.a = a + diff; m.b = b; m.c = c - diff; m.dist = dist+1; //printf("CAN GO c->a (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // c --> b if (c > 0 && b < b_cap) { int diff = min(c, b_cap - b); node_with_dist m = node_with_dist(); m.a = a; m.b = b + diff; m.c = c - diff; m.dist = dist+1; //printf("CAN GO c->b (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } } void visit(int a, int b, int c, int dist) { node n = node(); n.a = a; n.b = b; n.c = c; visited_nodes.insert(n); update_scores(a, b, c, dist); check_possibilities(a, b, c, dist); } void run() { missing_scores = c_cap + 1; while (queue.size() > 0 && missing_scores > 0) { node_with_dist n = queue.front(); queue.pop_front(); //printf("Extract element from queue (size = %lu, missing_scores = %d); (%d, %d, %d); depth=%d\n", queue.size(), missing_scores, n.a, n.b, n.c, n.dist); visit(n.a, n.b, n.c, n.dist); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); for (int i = 0; i < MAXN; ++i) results[i] = NONE; int a, b, c; cin >> a_cap >> b_cap >> c_cap >> a >> b >> c; node_with_dist n = node_with_dist(); n.a = a; n.b = b; n.c = c; n.dist = 0; queue.push_back(n); run(); for (int i = 0; i <= c_cap; ++i) if (results[i] == NONE) results[i] = -1; //cout << "============== NODES =======================" << endl; //for (set<node>::iterator it = visited_nodes.begin(); it != visited_nodes.end(); it++) { // cout << it->a << " " << it->b << " " << it->c << endl; //} //cout << "============== RESULTS =======================" << endl; for (int i = 0; i <= c_cap; ++i) cout << results[i] << " "; cout << endl; }
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 155 156 157 158 | #include <vector> #include <iostream> #include <set> #include <map> #include <string> #include <list> using namespace std; #define MAXN 100010 #define NONE 2147483647 int a_cap, b_cap, c_cap; struct node { int a, b, c; bool operator < (const node &other) const { if (a == other.a) { if (b == other.b) { return c < other.c; } else { return b < other.b; } } else { return a < other.a; } } }; struct node_with_dist { int a, b, c, dist; }; set<node> visited_nodes; list<node_with_dist> queue; int missing_scores; int results[MAXN]; void update_scores(int a, int b, int c, int dist) { if (results[a] > dist) { //printf("Set first result for %d = %d\n", a, dist); results[a] = dist; missing_scores--; } if (results[b] > dist) { //printf("Set first result for %d = %d\n", b, dist); results[b] = dist; missing_scores--; } if (results[c] > dist) { //printf("Set first result for %d = %d\n", c, dist); results[c] = dist; missing_scores--; } } void queue_new(node_with_dist* nwd) { node n = node(); n.a = nwd->a; n.b = nwd->b; n.c = nwd->c; if (visited_nodes.find(n) == visited_nodes.end()) { queue.push_back(*nwd); } } void check_possibilities(int a, int b, int c, int dist) { // a --> b if (a > 0 && b < b_cap) { int diff = min(a, b_cap - b); node_with_dist m = node_with_dist(); m.a = a - diff; m.b = b + diff; m.c = c; m.dist = dist+1; //printf("CAN GO a->b (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // a --> c if (a > 0 && c < c_cap) { int diff = min(a, c_cap - c); node_with_dist m = node_with_dist(); m.a = a - diff; m.b = b; m.c = c + diff; m.dist = dist+1; //printf("CAN GO a->c (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // b --> a if (b > 0 && a < a_cap) { int diff = min(b, a_cap - a); node_with_dist m = node_with_dist(); m.a = a + diff; m.b = b - diff; m.c = c; m.dist = dist+1; //printf("CAN GO b->a (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // b --> c if (b > 0 && c < c_cap) { int diff = min(b, c_cap - c); node_with_dist m = node_with_dist(); m.a = a; m.b = b - diff; m.c = c + diff; m.dist = dist+1; //printf("CAN GO b->c (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // c --> a if (c > 0 && a < a_cap) { int diff = min(c, a_cap - a); node_with_dist m = node_with_dist(); m.a = a + diff; m.b = b; m.c = c - diff; m.dist = dist+1; //printf("CAN GO c->a (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } // c --> b if (c > 0 && b < b_cap) { int diff = min(c, b_cap - b); node_with_dist m = node_with_dist(); m.a = a; m.b = b + diff; m.c = c - diff; m.dist = dist+1; //printf("CAN GO c->b (%d, %d, %d) --> (%d, %d, %d); depth=%d\n", a, b, c, m.a, m.b, m.c, dist+1); queue_new(&m); } } void visit(int a, int b, int c, int dist) { node n = node(); n.a = a; n.b = b; n.c = c; visited_nodes.insert(n); update_scores(a, b, c, dist); check_possibilities(a, b, c, dist); } void run() { missing_scores = c_cap + 1; while (queue.size() > 0 && missing_scores > 0) { node_with_dist n = queue.front(); queue.pop_front(); //printf("Extract element from queue (size = %lu, missing_scores = %d); (%d, %d, %d); depth=%d\n", queue.size(), missing_scores, n.a, n.b, n.c, n.dist); visit(n.a, n.b, n.c, n.dist); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); for (int i = 0; i < MAXN; ++i) results[i] = NONE; int a, b, c; cin >> a_cap >> b_cap >> c_cap >> a >> b >> c; node_with_dist n = node_with_dist(); n.a = a; n.b = b; n.c = c; n.dist = 0; queue.push_back(n); run(); for (int i = 0; i <= c_cap; ++i) if (results[i] == NONE) results[i] = -1; //cout << "============== NODES =======================" << endl; //for (set<node>::iterator it = visited_nodes.begin(); it != visited_nodes.end(); it++) { // cout << it->a << " " << it->b << " " << it->c << endl; //} //cout << "============== RESULTS =======================" << endl; for (int i = 0; i <= c_cap; ++i) cout << results[i] << " "; cout << endl; } |