-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcpp_plain.cpp
59 lines (48 loc) · 1.15 KB
/
cpp_plain.cpp
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
56
57
58
59
#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>
struct Route
{
int dest;
int cost;
};
using Node = std::vector<Route>;
std::vector<char> visited;
std::vector<Node> nodes;
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[index] = false;
return max;
}
int main()
{
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});
}
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++plain/" << COMPILER << " " << ms.count() << "\n";
}