Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed cpp
Binary file not shown.
49 changes: 15 additions & 34 deletions cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include <ctime>
#include <vector>
#include <bitset>
#include <fstream>
#include <iostream>
#include <string>
#include <chrono>

using namespace std;
using namespace std::chrono;

struct node;

struct route{
int dest, cost;
node& dest;
const int cost;
};

struct node {
vector<route> neighbours;
bool visited = false;
};

vector<node> readPlaces(){
Expand All @@ -23,50 +24,30 @@ vector<node> readPlaces(){
vector<node> 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 T>
int getLongestPath(const vector<node> &nodes, const int nodeID, bitset<T> visited){
visited[nodeID] = true;
int getLongestPath(vector<node> &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<T>(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<node> &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<node> 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<microseconds>(end - start).count());
cout << len << " LANGUAGE C++ " << duration << std::endl;
Expand Down