forked from logicchains/LPATHBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdart.dart
More file actions
60 lines (52 loc) · 1.46 KB
/
dart.dart
File metadata and controls
60 lines (52 loc) · 1.46 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
import 'dart:io';
class Route {
final int dest, cost;
Route(this.dest, this.cost);
function toString(){
print("$dest, $cost\n");
}
}
class Node{
List<Route> neighbours;
Node(){
this.neighbours = new List<Route>();
}
}
readPlacesAndFindPath() {
var nodes;
new File('agraph').readAsLines().then((List<String> lines) {
final int numNodes = int.parse(lines[0]);
final nodes = new List<Node>.generate(numNodes, (int index) => new Node());
for(int i = 1; i < lines.length; i++){
final nums = lines[i].split(' ');
int node = int.parse(nums[0]);
int neighbour = int.parse(nums[1]);
int cost = int.parse(nums[2]);
nodes[node].neighbours.add(new Route(neighbour,cost));
}
var visited = new List<Bool>.generate(numNodes, (int index) => false);
var start = new DateTime.now();
int len = getLongestPath(nodes, 0, visited);
var duration = new DateTime.now().difference(start);
duration = duration.inMilliseconds;
print("$len LANGUAGE Dart $duration");
});
return nodes;
}
getLongestPath(List<Node> nodes, int nodeID, List<Bool> visited){
visited[nodeID] = true;
int max=0;
nodes[nodeID].neighbours.forEach((Route neighbour) {
if (!visited[neighbour.dest]){
final int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited);
if (dist > max){
max = dist;
}
}
});
visited[nodeID] = false;
return max;
}
main() {
readPlacesAndFindPath();
}