From e8ba701e3ad7b75652edf36e0bcd96b1c0b60c01 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sun, 30 Nov 2025 19:41:06 +0900 Subject: [PATCH] 20251130 baekjoon --- code_study/Baekjoon/python/1647.py | 42 ++++++++++ code_study/Baekjoon/swift/1197_1.swift | 48 +++++++++++ code_study/Baekjoon/swift/1197_2.swift | 108 +++++++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 code_study/Baekjoon/python/1647.py create mode 100644 code_study/Baekjoon/swift/1197_1.swift create mode 100644 code_study/Baekjoon/swift/1197_2.swift diff --git a/code_study/Baekjoon/python/1647.py b/code_study/Baekjoon/python/1647.py new file mode 100644 index 0000000..8f8007f --- /dev/null +++ b/code_study/Baekjoon/python/1647.py @@ -0,0 +1,42 @@ +import heapq +import sys + +input = sys.stdin.readline + +def solve() : + N, M = map(int, input().split()) + + graph = [[] for _ in range(N+1)] + + for _ in range(M) : + A, B, C = map(int, input().split()) + graph[A].append((B, C)) + graph[B].append((A, C)) + + pq = [] + visited = [False] * (N+1) + heapq.heappush(pq, (0,1)) + count = 0 + totalCost = 0 + maxCost = 0 + + while pq : + cost, node = heapq.heappop(pq) + + if visited[node] : continue + + visited[node] = True + count += 1 + totalCost += cost + maxCost = max(cost, maxCost) + + if count == N : break + + for next, n_cost in graph[node] : + if not visited[next] : + heapq.heappush(pq, (n_cost, next)) + + print(totalCost-maxCost) + +solve() + \ No newline at end of file diff --git a/code_study/Baekjoon/swift/1197_1.swift b/code_study/Baekjoon/swift/1197_1.swift new file mode 100644 index 0000000..539062c --- /dev/null +++ b/code_study/Baekjoon/swift/1197_1.swift @@ -0,0 +1,48 @@ +if let input1 = readLine(), + let nums1 = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], + let V = nums1.first, let E = nums1.last +{ + var edges: [(u: Int, v: Int, w: Int)] = [] + for _ in 1...E { + guard let input2 = readLine(), + let nums2 = input2.split(separator: " ").compactMap({Int($0)}) as? [Int], + nums2.count == 3 + else { break } + + let (u, v, w) = (nums2[0], nums2[1], nums2[2]) + edges.append((u: u, v: v, w: w)) + } + + edges.sort{$0.w < $1.w} + + var parents:[Int] = Array(0...V) + + func find(_ x: Int) -> Int { + if x != parents[x] { + parents[x] = find(parents[x]) + } + return parents[x] + } + + func union(_ x: Int, _ y: Int) { + let a = find(x) + let b = find(y) + + if a > b { + parents[a] = b + } + else { + parents[b] = a + } + } + + var total_weight = 0 + for (u, v, w) in edges { + if find(u) != find(v) { + total_weight += w + union(u, v) + } + } + + print(total_weight) +} diff --git a/code_study/Baekjoon/swift/1197_2.swift b/code_study/Baekjoon/swift/1197_2.swift new file mode 100644 index 0000000..c3b986b --- /dev/null +++ b/code_study/Baekjoon/swift/1197_2.swift @@ -0,0 +1,108 @@ +struct MinHeap { + var nodes: [T] = [] + let sort: (T, T) -> Bool + + init(sort: @escaping (T, T) -> Bool) { + self.sort = sort + } + + var isEmpty: Bool { + return nodes.isEmpty + } + + mutating func insert(_ element: T) { + nodes.append(element) + shiftUp(from: nodes.count - 1) + } + + mutating func pop() -> T? { + if nodes.isEmpty { return nil } + if nodes.count == 1 { return nodes.removeLast() } + + let root = nodes[0] + nodes[0] = nodes.removeLast() + shiftDown(from: 0) + return root + } + + private mutating func shiftUp(from index: Int) { + var child = index + var parent = (child - 1) / 2 + + while child > 0 && sort(nodes[child], nodes[parent]) { + nodes.swapAt(child, parent) + child = parent + parent = (child - 1) / 2 + } + } + + private mutating func shiftDown(from index: Int) { + var parent = index + + while true { + let left = parent * 2 + 1 + let right = parent * 2 + 2 + var candidate = parent + + if left < nodes.count && sort(nodes[left], nodes[candidate]) { + candidate = left + } + if right < nodes.count && sort(nodes[right], nodes[candidate]) { + candidate = right + } + + if candidate == parent { return } + + nodes.swapAt(parent, candidate) + parent = candidate + } + } +} + +if let input1 = readLine(), + let nums1 = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], + let V = nums1.first, let E = nums1.last +{ + var graph: [[(node: Int, w: Int)]] = Array(repeating: [], count: V+1) + for _ in 1...E { + guard let input2 = readLine(), + let nums2 = input2.split(separator: " ").compactMap({Int($0)}) as? [Int], + nums2.count == 3 + else { break } + + let (u, v, w) = (nums2[0], nums2[1], nums2[2]) + graph[u].append((node: v, w: w)) + graph[v].append((node: u, w: w)) + } + + var pq = MinHeap<(node: Int, w: Int)>{$0.w < $1.w} + var visited = Array(repeating: false, count: V+1) + + pq.insert((node: 1, w: 0)) + var count = 0 + var totalWeight = 0 + + while !pq.isEmpty { + guard let current = pq.pop() else {break} + + if visited[current.node] { + continue + } + + visited[current.node] = true + count += 1 + totalWeight += current.w + + if count == V { + break + } + + for next in graph[current.node] { + if !visited[next.node] { + pq.insert(next) + } + } + } + + print(totalWeight) +}