diff --git a/cpp b/cpp deleted file mode 100755 index fcfdf2d..0000000 Binary files a/cpp and /dev/null differ diff --git a/cpp.cpp b/cpp.cpp index 5eae2f2..ca7a0b6 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -1,20 +1,21 @@ -#include #include -#include #include #include -#include #include using namespace std; using namespace std::chrono; +struct node; + struct route{ - int dest, cost; + node& dest; + const int cost; }; struct node { vector neighbours; + bool visited = false; }; vector readPlaces(){ @@ -23,50 +24,30 @@ vector readPlaces(){ vector nodes(numNodes); int node, neighbour, cost; while (text >> node >> neighbour >> cost){ - nodes[node].neighbours.push_back(route{neighbour, cost}); + nodes[node].neighbours.push_back(route{nodes[neighbour], cost}); } return nodes; } -template -int getLongestPath(const vector &nodes, const int nodeID, bitset visited){ - visited[nodeID] = true; +int getLongestPath(vector &nodes, node &node){ + node.visited = true; int max=0; - for(const route &neighbour: nodes[nodeID].neighbours){ - if (visited[neighbour.dest] == false){ - const int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited); + for(const route &neighbour: node.neighbours){ + if (!neighbour.dest.visited){ + const int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest); if (dist > max){ max = dist; } } } - visited[nodeID] = false; + node.visited = false; return max; } -int getLongestPath(const vector &nodes) -{ - if (nodes.size() <= 16) { - return getLongestPath<16>(nodes, 0, bitset<16>()); - } else if (nodes.size() <= 256) { - return getLongestPath<256>(nodes, 0, bitset<256>()); - } else if (nodes.size() <= 4096) { - return getLongestPath<4096>(nodes, 0, bitset<4096>()); - } else if (nodes.size() <= 65536) { - return getLongestPath<65536>(nodes, 0, bitset<65536>()); - } else if (nodes.size() <= 1048576) { - return getLongestPath<1048576>(nodes, 0, bitset<1048576>()); - } else if (nodes.size() <= 16777216) { - return getLongestPath<16777216>(nodes, 0, bitset<16777216>()); - } else { - return -1; - } -} - -int main(int argc, char** argv){ - auto nodes = readPlaces(); +int main() { + vector nodes = readPlaces(); auto start = high_resolution_clock::now(); - int len = getLongestPath(nodes); + int len = getLongestPath(nodes, nodes[0]); auto end = high_resolution_clock::now(); auto duration = (int)(0.001 * duration_cast(end - start).count()); cout << len << " LANGUAGE C++ " << duration << std::endl;