-
Notifications
You must be signed in to change notification settings - Fork 46
/
d.d
74 lines (62 loc) · 1.86 KB
/
d.d
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
import std.file;
import std.stdio;
import std.array;
import std.conv;
import std.string;
import std.datetime;
import std.exception;
import std.algorithm;
version = fast;
struct route{
int dest, cost;
}
struct node {
route[] neighbours;
}
node[] readPlaces(string text) pure {
auto lines = splitLines(text);
auto numNodes = to!int(lines.front);
lines.popFront();
node[] nodes = minimallyInitializedArray!(node[])(numNodes);
foreach(lineNum, string ln; lines){
auto nums = ln.splitter(" ").array.map!(to!int);
enforce(nums.length >= 3, "missing an edge on: " ~ (lineNum+1).to!string);
auto node = nums[0];
auto neighbour = nums[1];
auto cost = nums[2];
nodes[node].neighbours.insertInPlace(0, route(neighbour,cost));
}
return nodes;
}
int getLongestPath(immutable(node[]) nodes, const int nodeID, bool[] visited) nothrow @safe{
visited[nodeID] = true;
version(fast) {
int identifiedMax=0;
foreach(immutable route neighbour; nodes[nodeID].neighbours){
if (!visited[neighbour.dest]){
const int distance = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited);
identifiedMax = max(distance, identifiedMax);
}
}
} else {
// Slight increase to runtime for LDC
// Greater increase to runtime for DMD and GDC
int dist(immutable route r) nothrow @safe{
return r.cost + getLongestPath(nodes, r.dest, visited);
}
auto list = nodes[nodeID].neighbours.filter!(x=>!visited[x.dest]).map!dist;
auto identifiedMax = reduce!(max)(0, list);
}
visited[nodeID] = false;
return identifiedMax;
}
void main(){
immutable nodes = readPlaces(readText("agraph"));
auto visited = uninitializedArray!(bool[])(nodes.length);
visited[] = false;
StopWatch sw;
sw.start;
int len = getLongestPath(nodes, 0, visited);
sw.stop;
printf("%d LANGUAGE D %d\n", len, sw.peek().msecs);
}