From f54ef77fd15226ba6ee66d0e2f13770e022ebad9 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Thu, 25 Sep 2025 23:03:04 +0900 Subject: [PATCH] 20250925 baekjoon --- code_study/Baekjoon/swift/1504.swift | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 code_study/Baekjoon/swift/1504.swift diff --git a/code_study/Baekjoon/swift/1504.swift b/code_study/Baekjoon/swift/1504.swift new file mode 100644 index 0000000..aa2fbb9 --- /dev/null +++ b/code_study/Baekjoon/swift/1504.swift @@ -0,0 +1,83 @@ +let inf: Int = 987654321 + +func dijk(from start: Int,_ node: [[(Int,Int)]], _ n: Int) -> [Int] { + var dist: [Int] = Array(repeating: inf, count: n+1) + dist[start] = 0 + var qu = [(Int, Int)]() + qu.append((start, 0)) + + while !qu.isEmpty { + if let (now, weight) = qu.first { + qu.removeFirst() + + if dist[now] < weight { + continue + } + + for tup in node[now] { + if dist[tup.0] > tup.1 + weight { + dist[tup.0] = tup.1 + weight + qu.append((tup.0, dist[tup.0])) + } + } + } + } + + return dist +} + +if let input1 = readLine(), + let temp1 = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], + let N: Int = temp1.first, let E: Int = temp1.last +{ + var node: [[(Int, Int)]] = Array(repeating: [(Int, Int)](), count: N+1) + + for _ in 0..v1->v2->N + var path1: Int = -1 + if Dist_from1[v1] < inf && Dist_fromV1[v2] < inf && Dist_fromV2[N] < inf { + path1 = Dist_from1[v1] + Dist_fromV1[v2] + Dist_fromV2[N] + } + + //path2 1->v2->v1->N + var path2: Int = -1 + if Dist_from1[v2] < inf && Dist_fromV2[v1] < inf && Dist_fromV1[N] < inf { + path2 = Dist_from1[v2] + Dist_fromV2[v1] + Dist_fromV1[N] + } + + if path1 == -1 && path2 == -1 { + print(-1) + } + else if path1 == -1 || path2 == -1 { + print(max(path1, path2)) + } + else { + print(min(path1, path2)) + } + } +}