我是Haskell的新手。我編譯了代碼並打開了主shell。我不知道如何輸入圖形的邊緣並獲得輸出。任何幫助,將不勝感激。無法計算如何在Haskell編寫的Bellmanford代碼中輸入和輸出
給出圖中的圖和源頂點src,找到從src到給定圖中所有頂點的最短路徑。該圖可能包含負重邊。
{-# LANGUAGE BangPatterns #-}
module Main where
import Control.DeepSeq
import Data.Functor
import Data.Int
import Data.Vector.Unboxed ((//))
import qualified Data.Vector.Unboxed as V
--import Debug.Trace
type Vertex = Int
type Dist = Int32
type Edge = (Vertex, Vertex, Dist)
type EdgeVec = V.Vector Edge
type CostVec = V.Vector Dist
readEdge :: String -> Edge
readEdge s = let [v1, v2, w] = words s
in (read v1, read v2, read w)
bfStep :: EdgeVec -> CostVec -> CostVec
bfStep edges !prev = V.unsafeAccumulate min prev $ V.map mincost edges
where
mincost :: Edge -> (Int, Int32)
mincost (s, h, c) = (h, cost s c)
cost w c = let precost = prev `V.unsafeIndex` w
in if precost == maxBound then maxBound else precost + c
mkEdgeVec :: Int -> [String] -> EdgeVec
mkEdgeVec nvert inp = V.unfoldr step (nvert, inp)
where
step (n, s:xs) = Just (readEdge s, (n, xs))
step (0, []) = Nothing
step (!n, []) = Just ((0, n, 0), (n - 1, []))
main :: IO()
main = do
header:body <- lines <$> getContents
let nvert = read $ head $ words header
let edgelist = mkEdgeVec nvert body
let bfbase = V.replicate (nvert + 1) maxBound // [(0, 0)]
print $ edgelist `deepseq` "running"
let bfout = iterate (bfStep edgelist) bfbase !! nvert
let bfcheck = bfStep edgelist bfout
let hasCycle = V.any id $ V.zipWith (/=) bfout bfcheck
putStrLn $ if hasCycle then "Cycle" else show $ V.minimum bfout
我認爲「Bellmanford」是Bellman-Ford算法?不是一個完整的答案,因爲這聽起來像是作業,但是:作業是否指定了輸入格式? – Davislor
你會想創建一個包含數據的輸入文件,我們在程序所在的同一個目錄下調用它'input.txt',啓動一個控制檯,然後運行你的程序(如果可執行文件名爲'bellmanford')爲'bellmanford
Davislor