#define HEAP_MST 0
static const unsigned num_cache_elem = (MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2);
-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];
+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],
+ pess_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH];
inline unsigned short &cache(
unsigned row_from, unsigned switch_from, unsigned side_from,
row_to * MAX_SWITCH + switch_to];
}
+inline unsigned short &pess_cache(
+ unsigned row_from, unsigned switch_from,
+ unsigned row_to, unsigned switch_to)
+{
+ return pess_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
+ row_to * MAX_SWITCH + switch_to];
+}
+
struct order {
unsigned row, num;
int side;
return 41 * abs(from - to);
}
-int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
+int pessimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
{
- /* can we just walk directly? */
- if (row_from == row_to && side_from == side_to) {
- return distance_switch(switch_from, switch_to);
- }
-
- /* can we just switch sides? */
- if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
- return distance_switch(switch_from, switch_to);
- }
- if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
- return distance_switch(switch_from, switch_to);
- }
-
/* we'll need to go to one of the three middles */
int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
int distrow = distance_row(row_from, row_to);
}
}
+int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
+{
+ /* can we just walk directly? */
+ if (row_from == row_to && side_from == side_to) {
+ return distance_switch(switch_from, switch_to);
+ }
+
+ /* can we just switch sides? */
+ if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
+ return distance_switch(switch_from, switch_to);
+ }
+ if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
+ return distance_switch(switch_from, switch_to);
+ }
+
+ return pessimistic_distance(row_from, switch_from, row_to, switch_to);
+}
+
int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
{
- if (abs(row_from - row_to) == 1)
+ if (abs(row_from - row_to) <= 1)
return distance_switch(switch_from, switch_to);
else
- return distance(row_from, switch_from, 0, row_to, switch_to, 0);
+ return pessimistic_distance(row_from, switch_from, row_to, switch_to);
}
#if HEAP_MST
opt_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
optimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
+
+ pess_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
+ pessimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
}
}
-
+
order *ord = new order[points.size()];
best_tour = new order[points.size()];
order *temp = new order[points.size() * points.size() * 4];