๋ฌธ์ :
https://www.acmicpc.net/problem/1504
๋ถ์ :
์ด ๋ฌธ์ ๋ ๊ฑฐ์ณ์ผ ํ๋ ๋ ธ๋๊ฐ 2๊ฐ๋ก ํ์ ์ ์ด์ด์
start -> ๊ฑฐ์ณ์ผํ๋ ๋ ธ๋1 -> ๊ฑฐ์ณ์ผํ๋ ๋ ธ๋2 -> end
start -> ๊ฑฐ์ณ์ผํ๋ ๋ ธ๋2 -> ๊ฑฐ์ณ์ผํ๋ ๋ ธ๋1 -> end
๋ ์ค ์ต์๊ฐ์ ์ฐพ์ผ๋ฉด ๋๋ค !!
์ผ๋ฐ์ ์ธ ๋ค์ต์คํธ๋ผ๋ฅผ ๋ง๋ค๊ณ check() ํจ์๋ฅผ ํตํด ๋น๊ตํด์คฌ๋ค.
Swift ์ฝ๋ :
//
// main.swift
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2022/04/18.
//
import Foundation
var l = readLine()!.split(separator: " ").map { Int($0)! }
var n = l[0]
var e = l[1]
var map = Array(repeating: [(Int, Int)](), count: n+1) // (start, (end, value))
var d = [Int]()
var intMax = 987654321
for _ in 0..<e {
let line = readLine()!.split(separator: " ").map { Int($0)! }
let a = line[0]
let b = line[1]
let value = line[2]
map[a].append((b, value))
map[b].append((a, value))
}
var need = readLine()!.split(separator: " ").map { Int($0)! }
func dijikstra(start: Int, end: Int) -> Int {
d = Array(repeating: intMax, count: n+1)
d[start] = 0
var qu = [(Int, Int)]()
qu.append((0, start)) // ๊ฑฐ๋ฆฌ, ์์์
while !qu.isEmpty {
var cost = qu.first!.0
var now = qu.first!.1
qu.removeFirst()
for i in map[now] {
var c = cost + i.1
if d[i.0] > c {
d[i.0] = c
qu.append((c, i.0))
}
}
}
return d[end]
}
func check() -> Int {
var minValue = intMax
var start = dijikstra(start: 1, end: need[0])
start = start == intMax ? -1 : start
var middle = dijikstra(start: need[0], end: need[1])
middle = middle == intMax ? -1 : middle
var end = dijikstra(start: n, end: need[1])
end = end == intMax ? -1 : end
if start != -1 && middle != -1 && end != -1 {
minValue = start + middle + end
}
var start1 = dijikstra(start: 1, end: need[1])
start1 = start1 == intMax ? -1 : start1
var end1 = dijikstra(start: n, end: need[0])
end1 = end1 == intMax ? -1 : end1
if start1 != -1 && middle != -1 && end1 != -1 {
minValue = min(minValue, start1 + middle + end1)
}
return minValue == intMax ? -1 : minValue
}
print(check())
'Algorithm๐ฐ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] Swift AC implementation, ๋ฌธ์์ด (0) | 2022.05.13 |
---|---|
[๋ฐฑ์ค] Swift 11052 ์นด๋ ๊ตฌ๋งคํ๊ธฐ DP (0) | 2022.04.19 |
[๋ฐฑ์ค] Swift 14503 ๋ก๋ด ์ฒญ์๊ธฐ ์๋ฎฌ๋ ์ด์ , DFS (0) | 2022.04.18 |
[๋ฐฑ์ค] Swift ์ผ๋น ๋ฒ ์ด์ปจ์ 6๋จ๊ณ ๋ฒ์น BFS (0) | 2022.04.17 |
[๋ฐฑ์ค] Swift 2302 ๊ทน์ฅ ์ข์ DP (0) | 2022.04.17 |
๋๊ธ