Add solution for 2016 Day 24
This commit is contained in:
1 parent
edc2fd0b6c
commit
551df7ffb2
4 files changed
+94
-33
No files matched your search
+26
-22
@@ -506,17 +506,29 @@ struct Helper
|
||||
static void ResetTraceErrors();
|
||||
|
||||
private:
|
||||
template <typename Key, typename Value, typename Compare, typename Eval>
|
||||
template <typename Key, typename Value>
|
||||
static Value TSPCalculatePath(const Graph<Key, Value>& graph, const std::vector<Key>& path, bool returnToStart)
|
||||
{
|
||||
int cost = 0;
|
||||
for (int i = 0; i < path.size() - 1; i++)
|
||||
{
|
||||
cost += graph.GetEdge(path[i], path[i + 1]);
|
||||
}
|
||||
|
||||
if (returnToStart)
|
||||
cost += graph.GetEdge(path.back(), path.front());
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
template <typename Key, typename Value>
|
||||
static int TSP(const Graph<Key, Value>& graph,
|
||||
const std::set<Key>& nodesLeft,
|
||||
const std::vector<Key>& order,
|
||||
Compare compare,
|
||||
Eval eval)
|
||||
bool returnToStart)
|
||||
{
|
||||
if (nodesLeft.empty())
|
||||
{
|
||||
return eval(graph, order);
|
||||
}
|
||||
return TSPCalculatePath(graph, order, returnToStart);
|
||||
|
||||
bool hasSet = false;
|
||||
Value tsp;
|
||||
@@ -528,9 +540,9 @@ private:
|
||||
std::vector<Key> newOrder = order;
|
||||
newOrder.emplace_back(node);
|
||||
if (hasSet)
|
||||
tsp = compare(tsp, TSP(graph, newNodesLeft, newOrder, compare, eval));
|
||||
tsp = std::min(tsp, TSP(graph, newNodesLeft, newOrder, returnToStart));
|
||||
else
|
||||
tsp = TSP(graph, newNodesLeft, newOrder, compare, eval);
|
||||
tsp = TSP(graph, newNodesLeft, newOrder, returnToStart);
|
||||
hasSet = true;
|
||||
}
|
||||
return tsp;
|
||||
@@ -539,28 +551,20 @@ private:
|
||||
public:
|
||||
// Key - Node type of the graph
|
||||
// Value - Edge type of the graph
|
||||
// Compare - Value(Value, Value)
|
||||
// Given two solutions, which one is better
|
||||
// Eval - int(const Graph&, const std::vector<Key>&)
|
||||
// Given the graph and order of the TSP, what is the total cost
|
||||
template <typename Key, typename Value, typename Compare, typename Eval>
|
||||
static int TSP(const Graph<Key, Value>& graph, const Key& start, Compare compare, Eval eval)
|
||||
template <typename Key, typename Value>
|
||||
static int TSP(const Graph<Key, Value>& graph, const Key& start, bool returnToStart)
|
||||
{
|
||||
std::set<Key> nodes = graph.GetNodes();
|
||||
nodes.erase(nodes.find(start));
|
||||
return TSP(graph, nodes, std::vector<Key>{start}, compare, eval);
|
||||
return TSP(graph, nodes, std::vector<Key>{start}, returnToStart);
|
||||
}
|
||||
|
||||
// Key - Node type of the graph
|
||||
// Value - Edge type of the graph
|
||||
// Compare - Value(Value, Value)
|
||||
// Given two solutions, which one is better
|
||||
// Eval - int(const Graph&, const std::vector<Key>&)
|
||||
// Given the graph and order of the TSP, what is the total cost
|
||||
template <typename Key, typename Value, typename Compare, typename Eval>
|
||||
static int TSP(const Graph<Key, Value>& graph, Compare compare, Eval eval)
|
||||
template <typename Key, typename Value>
|
||||
static int TSP(const Graph<Key, Value>& graph, bool returnToStart)
|
||||
{
|
||||
return TSP(graph, graph.GetNodes(), std::vector<Key>{}, compare, eval);
|
||||
return TSP(graph, graph.GetNodes(), std::vector<Key>{}, returnToStart);
|
||||
}
|
||||
|
||||
static bool traceErrors;
|
||||
|
||||
Reference in new issue
Block a user