20251130 baekjoon
This commit is contained in:
parent
2cbd0981df
commit
e8ba701e3a
42
code_study/Baekjoon/python/1647.py
Normal file
42
code_study/Baekjoon/python/1647.py
Normal file
@ -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()
|
||||||
|
|
||||||
48
code_study/Baekjoon/swift/1197_1.swift
Normal file
48
code_study/Baekjoon/swift/1197_1.swift
Normal file
@ -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)
|
||||||
|
}
|
||||||
108
code_study/Baekjoon/swift/1197_2.swift
Normal file
108
code_study/Baekjoon/swift/1197_2.swift
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
struct MinHeap<T> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user