Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import Control.Applicative
  2. import Control.Monad
  3. import Data.List
  4. import Debug.Trace
  5.  
  6. road :: [Int] -> (Int, Int, Int)
  7. road x = (x !! 0, x !! 1, x !! 2)
  8.  
  9. city :: [Int] -> (Int, Int)
  10. city x = (x !! 0, x !! 1)
  11.  
  12. first :: (a, b, c) -> a
  13. first (a, _, _) = a
  14.  
  15. second :: (a, b, c) -> b
  16. second (_, b, _) = b
  17.  
  18. third :: (a, b, c) -> c
  19. third (_, _, c) = c
  20.  
  21. -------------------------------------------------------------------------------
  22.  
  23. --- (道路, 国民, 都市, 途中結果)
  24. func :: [(Int, Int, Int)] -> [(Int, Int, Int)] -> [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
  25. func roads people cities result = if null people then result else func invalidRoads (tail people) integratedCities refreshResult
  26. where
  27. man = head people
  28. tolerance = third man
  29. validRoads = foldr apply [] roads
  30. where apply road xs = if third road > tolerance then road : xs else []
  31. invalidRoads = tailLoop roads $ length validRoads
  32. integratedCities = integrate cities validRoads
  33. tailLoop roads num = if num == 0 then roads else tailLoop (tail roads) $ num - 1
  34. integrate c r = if null r then c else integrate (map (\city -> if (fst city) == (second (head r)) then (fst city, upstream (fst city) c) else city) c) $ (tail r)
  35. upstream index c = if (fst (c !! index)) == (snd (c !! index)) then index else upstream (snd (c !! index)) c
  36. possibleCitiesCount = length $ filter (\x -> (snd x) == (upstream (second man) integratedCities)) integratedCities
  37. refreshResult = map (\x -> if (fst x) == (first man) then (fst x, possibleCitiesCount) else x) $ traceShow result $ result
  38.  
  39. main :: IO ()
  40. main = do
  41. --- (#都市, #道路)
  42. [n, m] <- map read . words <$> getLine :: IO [Int]
  43. --- 道路: (都市s, 都市t, 年)
  44. roads <- replicateM m $ road . map read . words <$> getLine
  45.  
  46. --- #国民
  47. q <- readLn
  48. --- 国民: (都市, 年)
  49. people <- replicateM q $ city . map read . words <$> getLine
  50.  
  51. --- 国民に番号を振る
  52. let indexedPeople = map (\x -> (fst x, (fst (snd x)), (snd (snd x)))) $ zip [1..] people
  53. --- ソースが若い順にソート
  54. let sortedRoads = sortBy (\x y -> compare (second x) (second y)) roads
  55. --- 道路が若い順にソート
  56. let sortedPeople = sortBy (\x y -> compare (third y) (third x)) indexedPeople
  57.  
  58. --- index順に出力
  59. sequence $ map (\x -> putStrLn $ show $ snd x) $ sortBy (\x y -> compare (fst x) (fst y)) $ func sortedRoads sortedPeople (zip [1..] (take n (iterate (1 +) 1))) $ zip [1..] $ replicate q 0
  60.  
  61. return ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement