-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcpp.cpp
More file actions
54 lines (47 loc) · 1.2 KB
/
cpp.cpp
File metadata and controls
54 lines (47 loc) · 1.2 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
#include <vector>
#include <fstream>
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
struct node;
struct route{
node& dest;
const int cost;
};
struct node {
vector<route> neighbours;
bool visited = false;
};
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{nodes[neighbour], cost});
}
return nodes;
}
int getLongestPath(vector<node> &nodes, node &node){
node.visited = true;
int max=0;
for(const route &neighbour: node.neighbours){
if (!neighbour.dest.visited){
const int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest);
if (dist > max){
max = dist;
}
}
}
node.visited = false;
return max;
}
int main() {
vector<node> nodes = readPlaces();
auto start = high_resolution_clock::now();
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;
}