-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathjulia.jl
55 lines (42 loc) · 1.08 KB
/
julia.jl
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
immutable Route
dst :: Int
cost :: Int
end
type Node
neighbours :: Array{Route, 1}
end
function readPlaces(f)
n = int(readline(f))
nodes = Array(Node, n)
for i in 1:n
nodes[i] = Node([])
end
while !eof(f)
route = split(readline(f))
src = int(route[1]) + 1
dst = int(route[2]) + 1
cost = int(route[3])
push!(nodes[src].neighbours, Route(dst, cost))
end
n, nodes
end
function getLongestPath(nodes :: Array{Node, 1}, nodeid :: Int, visited :: Array{Bool, 1})
visited[nodeid] = true
m = 0
for neighbour in nodes[nodeid].neighbours
@inbounds if !visited[neighbour.dst]
const dist = neighbour.cost + getLongestPath(nodes, neighbour.dst, visited)
m = max(dist, m)
end
end
visited[nodeid] = false
m
end
function main()
n, nodes = open(readPlaces, "agraph")
visited = fill(false, n)
getLongestPath([Node([])], 1, visited)
t = @elapsed len = getLongestPath(nodes, 1, visited)
println(len, " LANGUAGE Julia ", int(t*1000), " ms")
end
main()