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 /* 4.1m per double row, plus gaps */
95 unsigned base_cost = 41 * abs(from - to);
97 if ((from <= 9) != (to <= 9))
99 if ((from <= 17) != (to <= 17))
101 if ((from <= 25) != (to <= 25))
103 if ((from <= 34) != (to <= 34))
106 /* don't calculate gaps here just yet, just estimate 4.1m per double row */
110 int pessimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
112 /* we'll need to go to one of the three middles */
113 int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
114 int distrow = distance_row(row_from, row_to);
115 if ((switch_from > 3) != (switch_to > 3))
116 return best2 + distrow;
117 if (switch_from > 3) {
118 int best3 = distance_middle(switch_from, 3) + distance_middle(switch_to, 3);
119 return std::min(best2, best3) + distrow;
121 int best1 = distance_middle(switch_from, 1) + distance_middle(switch_to, 1);
122 return std::min(best2, best1) + distrow;
126 int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
128 /* can we just walk directly? */
129 if (row_from == row_to && side_from == side_to) {
130 return distance_switch(switch_from, switch_to);
133 /* can we just switch sides? */
134 if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
135 return distance_switch(switch_from, switch_to);
137 if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
138 return distance_switch(switch_from, switch_to);
141 return pessimistic_distance(row_from, switch_from, row_to, switch_to);
144 int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
146 if (abs(row_from - row_to) <= 1)
147 return distance_switch(switch_from, switch_to);
149 return pessimistic_distance(row_from, switch_from, row_to, switch_to);
153 // this is, surprisingly enough, _slower_ than the naive variant below, so it's not enabled
154 struct prim_queue_val {
155 std::pair<unsigned, unsigned> dst;
158 bool operator< (const prim_queue_val &other) const
160 return (cost > other.cost);
164 // standard O(V^2 log v) prim
165 int prim_mst(std::set<std::pair<unsigned, unsigned> > &in)
167 std::set<std::pair<unsigned, unsigned> > set2;
168 std::priority_queue<prim_queue_val> queue;
170 // pick the first one
171 std::set<std::pair<unsigned, unsigned> >::iterator start = in.begin();
173 unsigned row = start->first;
174 unsigned num = start->second;
178 // find all the edges out from it
179 for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
183 unsigned d = opt_cache(row, num, j->first, j->second);
184 prim_queue_val val = { *j, d };
188 unsigned total_cost = 0;
189 while (set2.size() != in.size()) {
191 prim_queue_val val = queue.top();
194 // check if dst is already moved
195 if (set2.count(val.dst))
198 unsigned row = val.dst.first;
199 unsigned num = val.dst.second;
200 set2.insert(val.dst);
202 total_cost += val.cost;
204 // find all the edges from this new node
205 for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
209 unsigned d = opt_cache(row, num, j->first, j->second);
210 prim_queue_val val = { *j, d };
218 // extremely primitive O(V^3) prim
219 int prim_mst(std::set<std::pair<unsigned, unsigned> > &set1)
221 std::set<std::pair<unsigned, unsigned> > set2;
223 // pick the first one
224 std::set<std::pair<unsigned, unsigned> >::iterator start = set1.begin();
228 unsigned total_cost = 0;
229 while (set1.size() > 0) {
230 unsigned best_this_cost = UINT_MAX;
231 std::set<std::pair<unsigned, unsigned> >::iterator best_set1;
233 for (std::set<std::pair<unsigned, unsigned> >::iterator i = set1.begin(); i != set1.end(); ++i) {
234 for (std::set<std::pair<unsigned, unsigned> >::iterator j = set2.begin(); j != set2.end(); ++j) {
235 unsigned d = opt_cache(i->first, i->second, j->first, j->second);
236 if (d < best_this_cost) {
243 set2.insert(*best_set1);
244 set1.erase(best_set1);
245 total_cost += best_this_cost;
252 void print_tour(std::vector<std::pair<unsigned, unsigned> > &points)
254 std::set<std::pair<unsigned, unsigned> > points_left;
255 for (unsigned i = 0; i < points.size(); ++i) {
256 points_left.insert(points[i]);
259 for (unsigned i = 0; i < points.size(); ++i) {
260 if (best_tour[i].side == 0)
261 printf("%2u-%u (left side) ", best_tour[i].row, best_tour[i].num);
263 printf("%2u-%u (right side) ", best_tour[i].row, best_tour[i].num);
267 printf("cost=%4u ", best_tour[i].cost);
270 // let's see how good the MST heuristics are
271 if (i != points.size() - 1) {
272 std::set<std::pair<unsigned, unsigned> > mst_tree = points_left;
273 printf("mst_bound=%5u, ", prim_mst(mst_tree));
275 unsigned rest_cost = 0;
276 for (unsigned j = i + 1; j < points.size(); ++j) {
277 rest_cost += best_tour[j].cost;
280 printf("rest_cost=%5u", rest_cost);
285 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(best_tour[i].row, best_tour[i].num));
286 points_left.erase(j);
290 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)
292 if (cost_so_far >= best_so_far)
294 if (ind == points.size()) {
295 memcpy(best_tour, ord, sizeof(order) * points.size());
296 printf("\nNew best tour found! cost=%u\n", cost_so_far);
298 best_so_far = cost_so_far;
302 unsigned last_row = ord[ind-1].row;
303 unsigned last_switch = ord[ind-1].num;
304 unsigned last_side = ord[ind-1].side;
307 * The minimum spanning tree gives us a reliable lower bound for what we can
308 * achieve for the rest of the graph, so check it before doing anything else.
310 std::set<std::pair<unsigned, unsigned> > mst_set = points_left;
311 mst_set.insert(std::make_pair(last_row, last_switch));
313 unsigned min_rest_cost = prim_mst(mst_set);
314 if (cost_so_far + min_rest_cost >= best_so_far) {
319 * Simple heuristic: always search for the closest points from this one first; that
320 * will give us a sizable cutoff.
324 for (std::set<std::pair<unsigned, unsigned> >::iterator i = points_left.begin(); i != points_left.end(); ++i) {
326 temp[toi].row = i->first;
327 temp[toi].num = i->second;
329 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 0);
332 temp[toi].row = i->first;
333 temp[toi].num = i->second;
335 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 1);
338 std::sort(temp, temp + toi);
340 unsigned best_this_cost = UINT_MAX;
341 for (unsigned i = 0; i < toi; ++i)
345 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(temp[i].row, temp[i].num));
346 points_left.erase(j);
347 unsigned cost_rest = do_tsp(points, points_left, ord, temp + points.size() * 2, ind + 1, cost_so_far + temp[i].cost);
348 points_left.insert(std::make_pair(temp[i].row, temp[i].num));
350 best_this_cost = std::min(best_this_cost, cost_rest);
353 return best_this_cost;
358 std::vector<std::pair<unsigned, unsigned> > points;
359 std::set<std::pair<unsigned, unsigned> > points_left;
363 if (scanf("%u-%u", &row, &sw) != 2)
366 if (row < MIN_ROW || row > MAX_ROW || sw < MIN_SWITCH || sw > MAX_SWITCH) {
367 fprintf(stderr, "%u-%u is out of bounds!\n", row, sw);
371 points.push_back(std::make_pair(row, sw));
372 if (points.size() != 1)
373 points_left.insert(std::make_pair(row, sw));
376 // precalculate all distances
377 for (unsigned i = 0; i < points.size(); ++i) {
378 for (unsigned j = 0; j < points.size(); ++j) {
379 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0) =
380 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0);
382 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1) =
383 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1);
385 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0) =
386 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0);
388 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1) =
389 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1);
391 opt_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
392 optimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
394 pess_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
395 pessimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
399 order *ord = new order[points.size()];
400 best_tour = new order[points.size()];
401 order *temp = new order[points.size() * points.size() * 4];
403 /* always start at the first one, left side (hack) */
404 ord[0].row = points[0].first;
405 ord[0].num = points[0].second;
408 do_tsp(points, points_left, ord, temp, 1, 0);
409 printf("All done.\n");