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
62 lines (47 loc) · 1.49 KB
/
dart.dart
File metadata and controls
62 lines (47 loc) · 1.49 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
import 'dart:io';
import 'dart:math' as Math;
class Route {
final int dest, cost;
Route(this.dest, this.cost);
String toString() => "$dest $cost\n";
}
class Node{
final List<Route> neighbours = new List<Route>();
Node();
}
void readPlacesAndFindPath() {
List<String> lines = new File('agraph').readAsLinesSync();
int numNodes = int.parse(lines[0]);
List nodes = new List<Node>.generate(numNodes, (int index) => new Node());
for(int i = 1; i < lines.length; i++){
List 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);
print("$len LANGUAGE Dart ${duration.inMilliseconds}");
}
int getLongestPath(List<Node> nodes, int nodeID, List<bool> visited){
visited[nodeID] = true;
int max = 0;
final List<Route> neighbors = nodes[nodeID].neighbours;
for (var i = 0, llen = neighbors.length; i < llen; i++) {
final Route neighbor = neighbors[i];
final int dest = neighbor.dest;
if (!visited[dest]) {
final int dist = neighbor.cost +
getLongestPath(nodes, dest, visited);
max = Math.max(dist, max);
}
}
visited[nodeID] = false;
return max;
}
void main() {
readPlacesAndFindPath();
}