Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스
- 백트래킹
- 에라토스테네스의 체
- 위상정렬
- 백준
- 동적계획법
- 후니의 쉽게 쓴 시스코 네트워킹
- 그리디
- 수학
- 스택
- 투 포인터
- Kotlin
- 구현
- 유니온 파인드
- dfs
- 플로이드-와샬
- 문자열
- 시뮬레이션
- 이분탐색
- mst
- 완전탐색
- 세그먼트 트리
- java
- BFS
- Network
- CS
- 알고리즘
- JUnit 5
- swea
- Effective Java
Archives
반갑습니다!
[백준] 6497 전력난 본문
풀이
MST로 풀 수 있는 문제이다. 문제는 도시에서 절약할 수 있는 최대 액수를 구하는 것이고 이는 최소 액수로 가로등을 키는 문제라고 생각할 수 있다. 따라서 kruskal 알고리즘을 통해 가로등을 키는데 필요한 비용을 계산하고, 전체 비용에서 빼주면 된다.
코드
C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int m, n; | |
vector<pair<int, pair<int, int>>> edges; | |
int parent[200001]; | |
int find(int x) { | |
if (x == parent[x]) return x; | |
return parent[x] = find(parent[x]); | |
} | |
void merge(int a, int b) { | |
a = find(a); | |
b = find(b); | |
if (a < b) parent[b] = a; | |
else parent[a] = b; | |
} | |
int kruskal() { | |
sort(edges.begin(), edges.end()); | |
int sum = 0; | |
for (int i = 0; i < n; i++) { | |
int x = edges[i].second.first; | |
int y = edges[i].second.second; | |
int dist = edges[i].first; | |
if (find(x) == find(y)) continue; | |
merge(x, y); | |
sum += dist; | |
} | |
return sum; | |
} | |
int main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
while (true) { | |
cin >> m >> n; | |
if (m == 0 && n == 0) break; | |
edges.clear(); | |
for (int i = 1; i <= m; i++) parent[i] = i; | |
int total_dist = 0; | |
for (int i = 0; i < n; i++) { | |
int x, y, z; | |
cin >> x >> y >> z; | |
edges.push_back({ z, {x, y} }); | |
total_dist += z; | |
} | |
cout << total_dist - kruskal() << '\n'; | |
} | |
return 0; | |
} |
Python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin | |
def find(x): | |
if x != parent[x]: | |
parent[x] = find(parent[x]) | |
return parent[x] | |
def union(a, b): | |
a = find(a) | |
b = find(b) | |
if a < b: | |
parent[b] = a | |
else: | |
parent[a] = b | |
def kruskal(): | |
edges.sort() | |
sum = 0 | |
for i in edges: | |
x = i[1][0] | |
y = i[1][1] | |
dist = i[0] | |
if find(x) == find(y): | |
continue | |
sum += dist | |
union(x, y) | |
return sum | |
while True: | |
m, n = map(int, input.readline().rstrip().split()) | |
if m == 0 and n == 0: | |
break | |
parent = [0] * (m + 1) | |
edges = [] | |
for i in range(1, m + 1): | |
parent[i] = i | |
total_dist = 0 | |
for _ in range(n): | |
x, y, z = map(int, input.readline().rstrip().split()) | |
edges.append((z, (x, y))) | |
total_dist += z | |
print(total_dist - kruskal()) |
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 1915 가장 큰 정사각형 (0) | 2020.10.20 |
---|---|
[백준] 17472 다리 만들기 2 (0) | 2020.10.19 |
[백준] 4386 별자리 만들기 (0) | 2020.10.19 |
[백준] 14921용액 합성하기 (0) | 2020.10.14 |
[프로그래머스] 쿼드압축 후 개수 세기 (0) | 2020.10.13 |