-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcpp.cpp
More file actions
55 lines (49 loc) · 1.31 KB
/
cpp.cpp
File metadata and controls
55 lines (49 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define _POSIX_C_SOURCE 200809L
#include <chrono>
#include <vector>
#include <bitset>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
using namespace std::chrono;
struct route{
int dest, cost;
};
struct node {
vector<route> neighbours;
};
vector<node> readPlaces(){
ifstream text("agraph");
string numNodesText;
text >> numNodesText;
int numNodes = stoi(numNodesText);
vector<node> nodes(numNodes);
string nodeS, neighbourS, costS;
while (text >> nodeS >> neighbourS >> costS){
nodes[stoi(nodeS)].neighbours.push_back(route{ stoi(neighbourS), stoi(costS) });
}
return nodes;
}
inline int getLongestPath(vector<node> &nodes, int nodeID, vector<int> &visited){
visited[nodeID] = 1;
int max = 0;
for (const route& neighbour : nodes[nodeID].neighbours){
if (visited[neighbour.dest] == 0){
int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited);
if (dist > max){
max = dist;
}
}
}
visited[nodeID] = 0;
return max;
}
int main(int argc, char** argv){
auto nodes = readPlaces();
vector<int> visited(nodes.size(), 0);
auto start = high_resolution_clock::now();
int len = getLongestPath(nodes, 0, visited);
auto duration = high_resolution_clock::now() - start;
cout << len << " LANGUAGE C++ " << duration_cast<milliseconds>(duration).count() << "\n";
}