-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcpppruned.cpp
More file actions
104 lines (96 loc) · 2.69 KB
/
cpppruned.cpp
File metadata and controls
104 lines (96 loc) · 2.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <ctime>
#include <vector>
#include <bitset>
#include <fstream>
#include <iostream>
#include <string>
#include <chrono>
#include <algorithm>
using namespace std;
using namespace std::chrono;
struct route{
int dest, cost;
};
struct node {
vector<route> neighbours;
};
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});
}
for (auto &n : nodes) {
sort(n.neighbours.begin(), n.neighbours.end(),
[](const route &a, const route &b) {
return a.cost > b.cost;
});
}
return nodes;
}
static int getMaxCost(const vector<node> &nodes,
const vector<char> &visited) {
int result = 0;
int lowestMaxNodeCost = 0;
for (int i = 0; i < (int) nodes.size(); ++i) {
if (visited[i]) {
continue;
}
int maxNodeCost = 0;
for (const route &neighbour : nodes[i].neighbours) {
if (visited[neighbour.dest]) {
continue;
}
if (neighbour.cost > maxNodeCost) {
maxNodeCost = neighbour.cost;
}
}
if (maxNodeCost < lowestMaxNodeCost) {
lowestMaxNodeCost = maxNodeCost;
}
result += maxNodeCost;
}
return result - lowestMaxNodeCost;
}
static void getLongestPath(int remainingNodes,
int nodeID,
int curPathLen,
const vector<node> &nodes,
vector<char> &visited,
int &maxPathLen) {
if (!remainingNodes) {
if (curPathLen > maxPathLen) {
maxPathLen = curPathLen;
}
return;
}
visited[nodeID] = true;
int nextMaxCost = getMaxCost(nodes, visited);
for (const route &neighbour : nodes[nodeID].neighbours) {
if (!visited[neighbour.dest]) {
int nextPathLen = curPathLen + neighbour.cost;
if (nextPathLen + nextMaxCost > maxPathLen) {
getLongestPath(remainingNodes - 1, neighbour.dest, nextPathLen,
nodes, visited, maxPathLen);
}
}
}
visited[nodeID] = false;
}
static int getLongestPath(const vector<node> &nodes)
{
vector<char> visited(nodes.size());
int maxPathLen = 0;
getLongestPath(nodes.size() - 1, 0, 0, nodes, visited, maxPathLen);
return maxPathLen;
}
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++ " << duration << std::endl;
}