Created
January 24, 2013 10:35
-
-
Save josejuan/4619786 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Array.IO | |
import Control.Monad (forM_) | |
import System.Time | |
import System.IO | |
type IData = Int | |
-- slow but simple load (better avoiding intermediate list) | |
readWeights :: IO (IOUArray (Int, Int) IData) | |
readWeights = do | |
c <- getContents | |
let a :: [IData] | |
a = read c | |
s = (floor.sqrt.fromIntegral.length) a - 1 | |
newListArray ((0, 0), (s, s)) a | |
main = do | |
w <- readWeights | |
putStrLn "Computing distances..." | |
t0 <- getClockTime | |
((_, _), (b, _)) <- getBounds w | |
let r = [0..b] | |
-- can be parallelized!!! | |
forM_ r $ \k -> do | |
forM_ r $ \x -> do | |
forM_ r $ \y -> do | |
xk <- readArray w (x, k) | |
ky <- readArray w (k, y) | |
xy <- readArray w (x, y) | |
writeArray w (x, y) $ min xy (xk + ky) | |
t1 <- getClockTime | |
if b < 40 | |
then | |
forM_ [0..b] $ \x -> do | |
forM_ [0..b] $ \y -> do | |
readArray w (x, y) >>= putStr . (++ ", ") . show | |
putStrLn "|" | |
else | |
return () | |
putStrLn ("Time: " ++ show (diffClockTimes t1 t0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment