13 static const unsigned num_cache_elem = (MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2);
14 static unsigned short dist_cache[(MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2)],
15 opt_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH],
16 pess_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH];
18 inline unsigned short &cache(
19 unsigned row_from, unsigned switch_from, unsigned side_from,
20 unsigned row_to, unsigned switch_to, unsigned side_to)
22 return dist_cache[(row_from * MAX_SWITCH * 2 + switch_from * 2 + side_from) * (MAX_ROW * MAX_SWITCH * 2) +
23 row_to * MAX_SWITCH * 2 + switch_to * 2 + side_to];
26 inline unsigned short &opt_cache(
27 unsigned row_from, unsigned switch_from,
28 unsigned row_to, unsigned switch_to)
30 return opt_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
31 row_to * MAX_SWITCH + switch_to];
34 inline unsigned short &pess_cache(
35 unsigned row_from, unsigned switch_from,
36 unsigned row_to, unsigned switch_to)
38 return pess_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
39 row_to * MAX_SWITCH + switch_to];
47 bool operator< (const order &other) const
49 return (cost < other.cost);
53 static unsigned best_so_far = UINT_MAX;
56 int distance_switch(unsigned from, unsigned to)
58 /* on the same side of the middle? 9.6m per switch. */
59 if ((from > 3) == (to > 3)) {
60 return abs(from - to) * 96;
63 /* have to cross the border? 25.8m from sw3->sw4 => 16.2m extra gap. */
64 /* that's _got_ to be wrong. say it's 3m. */
65 return abs(from - to) * 96 + 30;
68 int distance_middle(unsigned sw, unsigned middle)
70 /* symmetry: 4-5-6 are just mirrored 3-2-1. */
75 /* estimate 25.8m/2 = 12.9m from sw3 to the middle */
76 return 129 + (3 - sw) * 96;
79 /* more symmetry -- getting from 1-6 to the top is like getting from 6-1 to the bottom. */
85 /* guesstimate 4.8m extra to get to the bottom */
87 return 48 + 162 + (sw - 1) * 96;
89 return 48 + (sw - 1) * 96;
92 int distance_row(unsigned from, unsigned to)
94 /* don't calculate gaps here just yet, just estimate 4.1m per double row */
95 return 41 * abs(from - to);
98 int pessimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
100 /* we'll need to go to one of the three middles */
101 int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
102 int distrow = distance_row(row_from, row_to);
103 if ((switch_from > 3) != (switch_to > 3))
104 return best2 + distrow;
105 if (switch_from > 3) {
106 int best3 = distance_middle(switch_from, 3) + distance_middle(switch_to, 3);
107 return std::min(best2, best3) + distrow;
109 int best1 = distance_middle(switch_from, 1) + distance_middle(switch_to, 1);
110 return std::min(best2, best1) + distrow;
114 int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
116 /* can we just walk directly? */
117 if (row_from == row_to && side_from == side_to) {
118 return distance_switch(switch_from, switch_to);
121 /* can we just switch sides? */
122 if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
123 return distance_switch(switch_from, switch_to);
125 if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
126 return distance_switch(switch_from, switch_to);
129 return pessimistic_distance(row_from, switch_from, row_to, switch_to);
132 int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
134 if (abs(row_from - row_to) <= 1)
135 return distance_switch(switch_from, switch_to);
137 return pessimistic_distance(row_from, switch_from, row_to, switch_to);
141 // this is, surprisingly enough, _slower_ than the naive variant below, so it's not enabled
142 struct prim_queue_val {
143 std::pair<unsigned, unsigned> dst;
146 bool operator< (const prim_queue_val &other) const
148 return (cost > other.cost);
152 // standard O(V^2 log v) prim
153 int prim_mst(std::set<std::pair<unsigned, unsigned> > &in)
155 std::set<std::pair<unsigned, unsigned> > set2;
156 std::priority_queue<prim_queue_val> queue;
158 // pick the first one
159 std::set<std::pair<unsigned, unsigned> >::iterator start = in.begin();
161 unsigned row = start->first;
162 unsigned num = start->second;
166 // find all the edges out from it
167 for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
171 unsigned d = opt_cache(row, num, j->first, j->second);
172 prim_queue_val val = { *j, d };
176 unsigned total_cost = 0;
177 while (set2.size() != in.size()) {
179 prim_queue_val val = queue.top();
182 // check if dst is already moved
183 if (set2.count(val.dst))
186 unsigned row = val.dst.first;
187 unsigned num = val.dst.second;
188 set2.insert(val.dst);
190 total_cost += val.cost;
192 // find all the edges from this new node
193 for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
197 unsigned d = opt_cache(row, num, j->first, j->second);
198 prim_queue_val val = { *j, d };
206 // extremely primitive O(V^3) prim
207 int prim_mst(std::set<std::pair<unsigned, unsigned> > &set1)
209 std::set<std::pair<unsigned, unsigned> > set2;
211 // pick the first one
212 std::set<std::pair<unsigned, unsigned> >::iterator start = set1.begin();
216 unsigned total_cost = 0;
217 while (set1.size() > 0) {
218 unsigned best_this_cost = UINT_MAX;
219 std::set<std::pair<unsigned, unsigned> >::iterator best_set1;
221 for (std::set<std::pair<unsigned, unsigned> >::iterator i = set1.begin(); i != set1.end(); ++i) {
222 for (std::set<std::pair<unsigned, unsigned> >::iterator j = set2.begin(); j != set2.end(); ++j) {
223 unsigned d = opt_cache(i->first, i->second, j->first, j->second);
224 if (d < best_this_cost) {
231 set2.insert(*best_set1);
232 set1.erase(best_set1);
233 total_cost += best_this_cost;
240 void print_tour(std::vector<std::pair<unsigned, unsigned> > &points)
242 std::set<std::pair<unsigned, unsigned> > points_left;
243 for (unsigned i = 0; i < points.size(); ++i) {
244 points_left.insert(points[i]);
247 for (unsigned i = 0; i < points.size(); ++i) {
248 if (best_tour[i].side == 0)
249 printf("%2u-%u (left side) ", best_tour[i].row, best_tour[i].num);
251 printf("%2u-%u (right side) ", best_tour[i].row, best_tour[i].num);
255 printf("cost=%4u ", best_tour[i].cost);
258 // let's see how good the MST heuristics are
259 if (i != points.size() - 1) {
260 std::set<std::pair<unsigned, unsigned> > mst_tree = points_left;
261 printf("mst_bound=%5u, ", prim_mst(mst_tree));
263 unsigned rest_cost = 0;
264 for (unsigned j = i + 1; j < points.size(); ++j) {
265 rest_cost += best_tour[j].cost;
268 printf("rest_cost=%5u", rest_cost);
273 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(best_tour[i].row, best_tour[i].num));
274 points_left.erase(j);
278 unsigned do_tsp(std::vector<std::pair<unsigned, unsigned> > &points, std::set<std::pair<unsigned, unsigned> > &points_left, order *ord, order *temp, unsigned ind, unsigned cost_so_far)
280 if (cost_so_far >= best_so_far)
282 if (ind == points.size()) {
283 memcpy(best_tour, ord, sizeof(order) * points.size());
284 printf("\nNew best tour found! cost=%u\n", cost_so_far);
286 best_so_far = cost_so_far;
291 * Simple heuristic: always search for the closest points from this one first; that
292 * will give us a sizable cutoff.
295 unsigned last_row = ord[ind-1].row;
296 unsigned last_switch = ord[ind-1].num;
297 unsigned last_side = ord[ind-1].side;
299 std::set<std::pair<unsigned, unsigned> > mst_set = points_left;
300 mst_set.insert(std::make_pair(last_row, last_switch));
302 for (std::set<std::pair<unsigned, unsigned> >::iterator i = points_left.begin(); i != points_left.end(); ++i) {
304 temp[toi].row = i->first;
305 temp[toi].num = i->second;
307 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 0);
310 temp[toi].row = i->first;
311 temp[toi].num = i->second;
313 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 1);
317 unsigned min_rest_cost = prim_mst(mst_set);
318 if (cost_so_far + min_rest_cost >= best_so_far) {
322 std::sort(temp, temp + toi);
324 unsigned best_this_cost = UINT_MAX;
325 for (unsigned i = 0; i < toi; ++i)
329 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(temp[i].row, temp[i].num));
330 points_left.erase(j);
331 unsigned cost_rest = do_tsp(points, points_left, ord, temp + points.size() * 2, ind + 1, cost_so_far + temp[i].cost);
332 points_left.insert(std::make_pair(temp[i].row, temp[i].num));
334 best_this_cost = std::min(best_this_cost, cost_rest);
337 return best_this_cost;
342 std::vector<std::pair<unsigned, unsigned> > points;
343 std::set<std::pair<unsigned, unsigned> > points_left;
347 if (scanf("%u-%u", &row, &sw) != 2)
350 if (row < MIN_ROW || row > MAX_ROW || sw < MIN_SWITCH || sw > MAX_SWITCH) {
351 fprintf(stderr, "%u-%u is out of bounds!\n", row, sw);
355 points.push_back(std::make_pair(row, sw));
356 if (points.size() != 1)
357 points_left.insert(std::make_pair(row, sw));
360 // precalculate all distances
361 for (unsigned i = 0; i < points.size(); ++i) {
362 for (unsigned j = 0; j < points.size(); ++j) {
363 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0) =
364 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0);
366 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1) =
367 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1);
369 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0) =
370 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0);
372 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1) =
373 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1);
375 opt_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
376 optimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
378 pess_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
379 pessimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
383 order *ord = new order[points.size()];
384 best_tour = new order[points.size()];
385 order *temp = new order[points.size() * points.size() * 4];
387 /* always start at the first one, left side (hack) */
388 ord[0].row = points[0].first;
389 ord[0].num = points[0].second;
392 do_tsp(points, points_left, ord, temp, 1, 0);
393 printf("All done.\n");