diff --git a/cpppruned.cpp b/cpppruned.cpp new file mode 100644 index 0000000..b2236e2 --- /dev/null +++ b/cpppruned.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono; + +struct route{ + int dest, cost; +}; + +struct node { + vector neighbours; +}; + +vector readPlaces(){ + ifstream text("agraph"); + int numNodes; text >> numNodes; + vector nodes(numNodes); + int node, neighbour, cost; + while (text >> node >> neighbour >> cost){ + nodes[node].neighbours.push_back(route{neighbour, cost}); + } + for (auto &n : nodes) { + sort(n.neighbours.begin(), n.neighbours.end(), + [](const route &a, const route &b) { + return a.cost > b.cost; + }); + } + return nodes; +} + +static int getMaxCost(const vector &nodes, + const vector &visited) { + int result = 0; + int lowestMaxNodeCost = 0; + for (int i = 0; i < (int) nodes.size(); ++i) { + if (visited[i]) { + continue; + } + int maxNodeCost = 0; + for (const route &neighbour : nodes[i].neighbours) { + if (visited[neighbour.dest]) { + continue; + } + if (neighbour.cost > maxNodeCost) { + maxNodeCost = neighbour.cost; + } + } + if (maxNodeCost < lowestMaxNodeCost) { + lowestMaxNodeCost = maxNodeCost; + } + result += maxNodeCost; + } + return result - lowestMaxNodeCost; +} + +static void getLongestPath(int remainingNodes, + int nodeID, + int curPathLen, + const vector &nodes, + vector &visited, + int &maxPathLen) { + if (!remainingNodes) { + if (curPathLen > maxPathLen) { + maxPathLen = curPathLen; + } + return; + } + visited[nodeID] = true; + int nextMaxCost = getMaxCost(nodes, visited); + for (const route &neighbour : nodes[nodeID].neighbours) { + if (!visited[neighbour.dest]) { + int nextPathLen = curPathLen + neighbour.cost; + if (nextPathLen + nextMaxCost > maxPathLen) { + getLongestPath(remainingNodes - 1, neighbour.dest, nextPathLen, + nodes, visited, maxPathLen); + } + } + } + visited[nodeID] = false; +} + +static int getLongestPath(const vector &nodes) +{ + vector visited(nodes.size()); + int maxPathLen = 0; + getLongestPath(nodes.size() - 1, 0, 0, nodes, visited, maxPathLen); + return maxPathLen; +} + +int main(int argc, char** argv){ + auto nodes = readPlaces(); + auto start = high_resolution_clock::now(); + int len = getLongestPath(nodes); + auto end = high_resolution_clock::now(); + auto duration = (int)(0.001 * duration_cast(end - start).count()); + cout << len << " LANGUAGE C++ " << duration << std::endl; +}