-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcrystal.cr
49 lines (37 loc) · 999 Bytes
/
crystal.cr
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
record Route, dest, cost
struct Node
getter neighbours
def initialize
@neighbours = [] of Route
end
end
def read_places
lines = File.read_lines("agraph")
num_nodes = lines.shift.to_i
nodes = Array.new(num_nodes) { Node.new }
lines.each do |line|
nums = line.split.map &.to_i
break if nums.length < 3
node, neighbour, cost = nums
nodes[node].neighbours << Route.new(neighbour, cost)
end
nodes
end
def get_longest_path(nodes, node_id, visited)
max = 0
visited[node_id] = true
nodes[node_id].neighbours.each do |neighbour|
unless visited[neighbour.dest]
dist = neighbour.cost + get_longest_path(nodes, neighbour.dest, visited)
max = dist if dist > max
end
end
visited[node_id] = false
max
end
nodes = read_places
visited = Array.new(nodes.length, false)
time = Time.now
len = get_longest_path nodes, 0, visited
duration = Time.now - time
STDOUT << len << " LANGUAGE CRYSTAL " << duration.total_milliseconds.to_i << '\n'