|
|
@ -1,15 +1,25 @@
|
|
|
|
module LeastReachableCity where
|
|
|
|
module LeastReachableCity(dijkstra, leastReachableCity) where
|
|
|
|
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Types
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Well, it's just a magic number, because it's too much hassle to implement type like "Len val | Infinity"
|
|
|
|
|
|
|
|
-- TODO: get graph radius and use this number as infinity
|
|
|
|
infinity :: Len
|
|
|
|
infinity :: Len
|
|
|
|
infinity = 999999 :: Len
|
|
|
|
infinity = 999999 :: Len
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leastReachableCity :: Graph -> Len -> (Point, Int)
|
|
|
|
|
|
|
|
leastReachableCity graph mileage = foldl (\(p, n) (p', n') ->
|
|
|
|
|
|
|
|
if n' < n then (p',n')
|
|
|
|
|
|
|
|
else (p, n))
|
|
|
|
|
|
|
|
(-1, infinity) [(city, countReachableCities $ dijkstra city graph) | city <- Set.toList $ fst $ countVertices graph] where
|
|
|
|
|
|
|
|
countReachableCities :: Distances -> Int
|
|
|
|
|
|
|
|
countReachableCities distances = Map.foldl (\n val -> if (val <= mileage) && (val /= 0) then n + 1 else n) 0 distances
|
|
|
|
|
|
|
|
|
|
|
|
dijkstra :: Point -> Graph -> Distances
|
|
|
|
dijkstra :: Point -> Graph -> Distances
|
|
|
|
dijkstra start graph = dijkstra' start graph visited_init to_visit_init distances_init where
|
|
|
|
dijkstra start graph = dijkstra' start graph visited_init to_visit_init distances_init where
|
|
|
|
-- Helper
|
|
|
|
-- Helper (so we don' need to call dijkstra initializing empty visited, to_visit etc)
|
|
|
|
dijkstra' :: Point -> Graph -> Vertices -> Vertices -> Distances -> Distances
|
|
|
|
dijkstra' :: Point -> Graph -> Vertices -> Vertices -> Distances -> Distances
|
|
|
|
dijkstra' start graph visited to_visit distances = if Set.null to_visit
|
|
|
|
dijkstra' start graph visited to_visit distances = if Set.null to_visit
|
|
|
|
then
|
|
|
|
then
|
|
|
@ -22,9 +32,9 @@ dijkstra start graph = dijkstra' start graph visited_init to_visit_init distance
|
|
|
|
(Set.union (Set.delete start to_visit) $ findNotVisitedNeighbours graph visited start)
|
|
|
|
(Set.union (Set.delete start to_visit) $ findNotVisitedNeighbours graph visited start)
|
|
|
|
(updateDistances graph start distances)
|
|
|
|
(updateDistances graph start distances)
|
|
|
|
-- Other
|
|
|
|
-- Other
|
|
|
|
to_visit_init = (Set.insert start $ Set.empty :: Set.Set Point)
|
|
|
|
to_visit_init = (Set.insert start $ Set.empty :: Vertices)
|
|
|
|
distances_init = (infinityDistances start $ countVertices graph)
|
|
|
|
distances_init = (infinityDistances start $ snd $ countVertices graph)
|
|
|
|
visited_init = (Set.empty :: Set.Set Point)
|
|
|
|
visited_init = (Set.empty :: Vertices)
|
|
|
|
|
|
|
|
|
|
|
|
updateDistances :: Graph -> Point -> Distances -> Distances
|
|
|
|
updateDistances :: Graph -> Point -> Distances -> Distances
|
|
|
|
updateDistances graph point distances = snd $ Map.foldrWithKey decideMin ((point, Map.findWithDefault (-1) point distances), Map.empty :: Distances) distances where
|
|
|
|
updateDistances graph point distances = snd $ Map.foldrWithKey decideMin ((point, Map.findWithDefault (-1) point distances), Map.empty :: Distances) distances where
|
|
|
@ -42,13 +52,13 @@ findMinNotVisited to_visit distances = fst $ Set.foldl (\(p, l) p' -> case Map.l
|
|
|
|
(Just l') -> if l' < l then
|
|
|
|
(Just l') -> if l' < l then
|
|
|
|
(p', l')
|
|
|
|
(p', l')
|
|
|
|
else (p,l))
|
|
|
|
else (p,l))
|
|
|
|
(-1, maxBound) to_visit
|
|
|
|
(-1, infinity) to_visit
|
|
|
|
|
|
|
|
|
|
|
|
infinityDistances :: Point -> Int -> Distances
|
|
|
|
infinityDistances :: Point -> Int -> Distances
|
|
|
|
infinityDistances point n = Map.fromList $ (point, 0) : [(x, infinity) | x <- [1..n], x /= point]
|
|
|
|
infinityDistances point n = Map.fromList $ (point, 0) : [(x, infinity) | x <- [1..n], x /= point]
|
|
|
|
|
|
|
|
|
|
|
|
countVertices :: Graph -> Int
|
|
|
|
countVertices :: Graph -> (Vertices, Int)
|
|
|
|
countVertices graph = snd $ foldl f (Set.empty :: Set.Set Point, 0) graph where
|
|
|
|
countVertices graph = foldl f (Set.empty :: Vertices, 0) graph where
|
|
|
|
f :: (Vertices, Int) -> Node -> (Vertices, Int)
|
|
|
|
f :: (Vertices, Int) -> Node -> (Vertices, Int)
|
|
|
|
f (vertices, n) (Node (a, b, _)) =
|
|
|
|
f (vertices, n) (Node (a, b, _)) =
|
|
|
|
let aIsNotMember = not $ Set.member a vertices
|
|
|
|
let aIsNotMember = not $ Set.member a vertices
|
|
|
|