Problem 81(Dijkstra)
コンテンツ
Dijkstraで解く。
import Dijkstra import Data.Graph import Data.Array.IArray w = 80 xi (i,j) = w*i+j mkGraph = buildG (,w*w).((w*w,):).concatMap e$range b where b = ((,),(w-1,w-1)) e (i,j) = [(xi (i,j),xi x)|x<-[(i+1,j),(i,j+1)],inRange b x] mkWeight a = array b .(s:).concatMap e $range b'::Weight where b = ((,),(w*w,w*w)) b' = ((,),(w-1,w-1)) e (i,j) = [((xi (i,j),xi x),a!x)|x<-[(i+1,j),(i,j+1)],inRange b' x] s = ((w*w,),a!(,)) mkArray ::String->Array (Int,Int) Int mkArray ws= listArray ((,),(w-1,w-1)) ws' where ws'=concat.map (read.g).lines$ws g x = "["++x++"]" main =do f<-readFile "matrix.txt" print.fst.shortestPath mkGraph (mkWeight$mkArray f) (w*w) $w*w-1
DPのほうが速かった件について
作成者 Toru Mano
最終更新時刻 2023-01-01 (c70d5a1)