12 static const unsigned num_cache_elem = (MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2);
13 static unsigned short dist_cache[(MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2)], opt_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH];
15 inline unsigned short &cache(
16 unsigned row_from, unsigned switch_from, unsigned side_from,
17 unsigned row_to, unsigned switch_to, unsigned side_to)
19 return dist_cache[(row_from * MAX_SWITCH * 2 + switch_from * 2 + side_from) * (MAX_ROW * MAX_SWITCH * 2) +
20 row_to * MAX_SWITCH * 2 + switch_to * 2 + side_to];
23 inline unsigned short &opt_cache(
24 unsigned row_from, unsigned switch_from,
25 unsigned row_to, unsigned switch_to)
27 return opt_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
28 row_to * MAX_SWITCH + switch_to];
36 bool operator< (const order &other) const
38 return (cost < other.cost);
42 static unsigned best_so_far = UINT_MAX;
45 int distance_switch(unsigned from, unsigned to)
47 /* on the same side of the middle? 9.6m per switch. */
48 if ((from > 3) == (to > 3)) {
49 return abs(from - to) * 96;
52 /* have to cross the border? 25.8m from sw3->sw4 => 16.2m extra gap. */
53 /* that's _got_ to be wrong. say it's 3m. */
54 return abs(from - to) * 96 + 30;
57 int distance_middle(unsigned sw, unsigned middle)
59 /* symmetry: 4-5-6 are just mirrored 3-2-1. */
64 /* estimate 25.8m/2 = 12.9m from sw3 to the middle */
65 return 129 + (3 - sw) * 96;
68 /* more symmetry -- getting from 1-6 to the top is like getting from 6-1 to the bottom. */
74 /* guesstimate 4.8m extra to get to the bottom */
76 return 48 + 162 + (sw - 1) * 96;
78 return 48 + (sw - 1) * 96;
81 int distance_row(unsigned from, unsigned to)
83 /* don't calculate gaps here just yet, just estimate 4.1m per double row */
84 return 41 * abs(from - to);
87 int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
89 /* can we just walk directly? */
90 if (row_from == row_to && side_from == side_to) {
91 return distance_switch(switch_from, switch_to);
94 /* can we just switch sides? */
95 if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
96 return distance_switch(switch_from, switch_to);
98 if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
99 return distance_switch(switch_from, switch_to);
102 /* we'll need to go to one of the three middles */
103 int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
104 int distrow = distance_row(row_from, row_to);
105 if ((switch_from > 3) != (switch_to > 3))
106 return best2 + distrow;
107 if (switch_from > 3) {
108 int best3 = distance_middle(switch_from, 3) + distance_middle(switch_to, 3);
109 return std::min(best2, best3) + distrow;
111 int best1 = distance_middle(switch_from, 1) + distance_middle(switch_to, 1);
112 return std::min(best2, best1) + distrow;
116 int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
118 if (abs(row_from - row_to) == 1)
119 return distance_switch(switch_from, switch_to);
121 return distance(row_from, switch_from, 0, row_to, switch_to, 0);
124 // extremely primitive O(V^3) prim
125 int prim_mst(std::set<std::pair<unsigned, unsigned> > &set1)
127 std::set<std::pair<unsigned, unsigned> > set2;
129 // pick the first one
130 std::set<std::pair<unsigned, unsigned> >::iterator start = set1.begin();
134 unsigned total_cost = 0;
135 while (set1.size() > 0) {
136 unsigned best_this_cost = UINT_MAX;
137 std::set<std::pair<unsigned, unsigned> >::iterator best_set1;
139 for (std::set<std::pair<unsigned, unsigned> >::iterator i = set1.begin(); i != set1.end(); ++i) {
140 for (std::set<std::pair<unsigned, unsigned> >::iterator j = set2.begin(); j != set2.end(); ++j) {
141 unsigned d = opt_cache(i->first, i->second, j->first, j->second);
142 if (d < best_this_cost) {
149 set2.insert(*best_set1);
150 set1.erase(best_set1);
151 total_cost += best_this_cost;
158 void print_tour(std::vector<std::pair<unsigned, unsigned> > &points)
160 std::set<std::pair<unsigned, unsigned> > points_left;
161 for (unsigned i = 0; i < points.size(); ++i) {
162 points_left.insert(points[i]);
165 for (unsigned i = 0; i < points.size(); ++i) {
166 if (best_tour[i].side == 0)
167 printf("%2u-%u (left side) ", best_tour[i].row, best_tour[i].num);
169 printf("%2u-%u (right side) ", best_tour[i].row, best_tour[i].num);
173 printf("cost=%4u ", best_tour[i].cost);
176 // let's see how good the MST heuristics are
177 if (i != points.size() - 1) {
178 std::set<std::pair<unsigned, unsigned> > mst_tree = points_left;
179 printf("mst_bound=%5u, ", prim_mst(mst_tree));
181 unsigned rest_cost = 0;
182 for (unsigned j = i + 1; j < points.size(); ++j) {
183 rest_cost += best_tour[j].cost;
186 printf("rest_cost=%5u", rest_cost);
191 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(best_tour[i].row, best_tour[i].num));
192 points_left.erase(j);
196 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)
198 if (cost_so_far >= best_so_far)
200 if (ind == points.size()) {
201 memcpy(best_tour, ord, sizeof(order) * points.size());
202 printf("\nNew best tour found! cost=%u\n", cost_so_far);
204 best_so_far = cost_so_far;
209 * Simple heuristic: always search for the closest points from this one first; that
210 * will give us a sizable cutoff.
213 unsigned last_row = ord[ind-1].row;
214 unsigned last_switch = ord[ind-1].num;
215 unsigned last_side = ord[ind-1].side;
217 std::set<std::pair<unsigned, unsigned> > mst_set = points_left;
218 mst_set.insert(std::make_pair(last_row, last_switch));
220 for (std::set<std::pair<unsigned, unsigned> >::iterator i = points_left.begin(); i != points_left.end(); ++i) {
222 temp[toi].row = i->first;
223 temp[toi].num = i->second;
225 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 0);
228 temp[toi].row = i->first;
229 temp[toi].num = i->second;
231 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 1);
235 unsigned min_rest_cost = prim_mst(mst_set);
236 if (cost_so_far + min_rest_cost >= best_so_far) {
240 std::sort(temp, temp + toi);
242 unsigned best_this_cost = UINT_MAX;
243 for (unsigned i = 0; i < toi; ++i)
247 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(temp[i].row, temp[i].num));
248 points_left.erase(j);
249 unsigned cost_rest = do_tsp(points, points_left, ord, temp + points.size() * 2, ind + 1, cost_so_far + temp[i].cost);
250 points_left.insert(std::make_pair(temp[i].row, temp[i].num));
252 best_this_cost = std::min(best_this_cost, cost_rest);
255 return best_this_cost;
260 std::vector<std::pair<unsigned, unsigned> > points;
261 std::set<std::pair<unsigned, unsigned> > points_left;
265 if (scanf("%u-%u", &row, &sw) != 2)
268 if (row < MIN_ROW || row > MAX_ROW || sw < MIN_SWITCH || sw > MAX_SWITCH) {
269 fprintf(stderr, "%u-%u is out of bounds!\n", row, sw);
273 points.push_back(std::make_pair(row, sw));
274 if (points.size() != 1)
275 points_left.insert(std::make_pair(row, sw));
278 // precalculate all distances
279 for (unsigned i = 0; i < points.size(); ++i) {
280 for (unsigned j = 0; j < points.size(); ++j) {
281 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0) =
282 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0);
284 cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1) =
285 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1);
287 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0) =
288 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0);
290 cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1) =
291 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1);
293 opt_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
294 optimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
298 order *ord = new order[points.size()];
299 best_tour = new order[points.size()];
300 order *temp = new order[points.size() * points.size() * 4];
302 /* always start at the first one, left side (hack) */
303 ord[0].row = points[0].first;
304 ord[0].num = points[0].second;
307 do_tsp(points, points_left, ord, temp, 1, 0);
308 printf("All done.\n");