Skip to content

Rewrite the cpp example to do the same thing as the nim example #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
106 changes: 46 additions & 60 deletions cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,59 @@
#include <ctime>
#include <vector>
#include <bitset>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <chrono>

using namespace std;
using namespace std::chrono;
#include <vector>

struct route{
int dest, cost;
struct Route
{
int dest;
int cost;
};

struct node {
vector<route> neighbours;
};
using Node = std::vector<Route>;

vector<node> readPlaces(){
ifstream text("agraph");
int numNodes; text >> numNodes;
vector<node> nodes(numNodes);
int node, neighbour, cost;
while (text >> node >> neighbour >> cost){
nodes[node].neighbours.push_back(route{neighbour, cost});
}
return nodes;
}
std::vector<char> visited;
std::vector<Node> nodes;

template <int T>
int getLongestPath(const vector<node> &nodes, const int nodeID, bitset<T> visited){
visited[nodeID] = 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);
if (dist > max){
max = dist;
}
static
int GetLongestPath(int index)
{
int max = 0;
visited[index] = true;

for (auto neighbour : nodes[index])
{
if (!visited[neighbour.dest])
{
auto dist = neighbour.cost + GetLongestPath(neighbour.dest);
max = std::max(max, dist);
}
}
}
visited[nodeID] = false;
return max;

visited[index] = false;
return max;
}

int getLongestPath(const vector<node> &nodes)

int main()
{
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;
}
}
std::ifstream in("agraph");

int num_nodes;
in >> num_nodes;
nodes.resize(num_nodes);
visited.resize(num_nodes);

int index;
int neighbour;
int cost;
while (in >> index >> neighbour >> cost)
{
nodes[index].push_back({neighbour, cost});
}

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<microseconds>(end - start).count());
cout << len << " LANGUAGE C++/" << COMPILER << " " << duration << std::endl;
auto start = std::chrono::steady_clock::now();
auto len = GetLongestPath(0);
auto stop = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << len << " LANGUAGE C++/" << COMPILER << " " << ms.count() << "\n";
}