์ต์์ ์ฅํธ๋ฆฌ(MST / Minimum Spanning Tree)๋ ๋ฐฉํฅ์ฑ์ด ์๋ ๊ฐ์ค ๊ทธ๋ํ์์ ๊ฐ์ ์ ๊ฐ์ค์น์ ํฉ์ด ์ต์์ธ ์ ์ฅ ํธ๋ฆฌ์ด๋ค.

๐น๏ธ ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ & ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ ๊ณผ์
๐น ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ : ๊ฐ์ ์ค์ฌ์ผ๋ก ์ต์ ์ ์ฅ ํธ๋ฆฌ ๊ตฌํ๊ธฐ (๊ฐ์ ์ ๊ฐ์๊ฐ ์ ๋ค๋ฉด ํ๋ฆผ๋ณด๋ค ํจ์จ์ !)
- ๊ฐ์ ๋ค์ ๊ฐ์ค์น๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ ํ ๊ฐ์ค์น๊ฐ ๋ฎ์ ๊ฐ์ ๋ถํฐ ์ ํํ๋ค.
- ์ ํํ ๊ฐ์ ์ผ๋ก ์ธํด ์ฌ์ดํด์ด ํ์ฑ๋๋ค๋ฉด ๋ค์ ๊ฐ์ ์ผ๋ก ๋์ด๊ฐ๊ณ , ์ฌ์ดํด์ด ํ์ฑ๋์ง ์๋๋ค๋ฉด ํด๋น ๊ฐ์ ์ ์ ํํ๋ค. โ ์ฌ์ดํด์ด ํ์ฑ๋๋์ง ํ์ ํ๊ธฐ ์ํด Union-Find ์ฐ์ฐ์ ์งํํ๋ค.
- ์ ์ ์ด N๊ฐ์ธ ๊ทธ๋ํ์ผ ๋, N-1๊ฐ์ ๊ฐ์ ์ ์ ํํ๋ค๋ฉด ๋ฐ๋ณต๋ฌธ์ ์ข ๋ฃํ๋ค.
๐น ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ : ์ ์ ์ค์ฌ์ผ๋ก ์ต์ ์ ์ฅ ํธ๋ฆฌ ๊ตฌํ๊ธฐ (์ ์ ์ ๊ฐ์๊ฐ ์ ๋ค๋ฉด ํฌ๋ฃจ์ค์นผ๋ณด๋ค ํจ์จ์ !)
- ์์์ ์ ์ ์ ์ ํํ๋ค.
- ์ ํํ ์ ์ ๊ณผ ์ฐ๊ฒฐ๋ ๊ฐ์ ๋ค ์ค์์ ๊ฐ์ค์น๊ฐ ๋ฎ์ ๊ฐ์ ์ ์ ํํ๋ค.
- ๋ชจ๋ ์ ์ ์ด ์ ํ๋ ๋๊น์ง ๋ฐ๋ณตํ๋ค. โ ์ ์ ์ ๋ฐฉ๋ฌธํ๋์ง ์ฌ๋ถ๋ฅผ ํ์ ํ๊ธฐ ์ํด boolean[] visit์ ์ฌ์ฉํ๋ค.
๐ฉ๐ป ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ ์ฝ๋ ๊ตฌํ
public class BOJ_1197_์ต์์คํจ๋ํธ๋ฆฌ {
static int[] parent;
static int v,e;
static PriorityQueue<Node> pq;
// ๊ฐ์ค์น๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ โ PriorityQueue ์ฌ์ฉ
static class Node implements Comparable<Node>{
int from, to, weight;
public Node(int from, int to, int weight){
this.from = from;
this.to = to;
this.weight = weight;
}
public int compareTo(Node n){
return this.weight-n.weight;
}
}
/*
Union-Find ์ฐ์ฐ
*/
static void union(int x, int y){
x = find(x);
y = find(y);
if(x!=y){
parent[y]=x;
}
}
static int find(int x){
if(parent[x]==x) return x;
return parent[x]=find(parent[x]);
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
pq = new PriorityQueue<>();
parent = new int[v+1];
// ๋ถ๋ชจ๋ฅผ ๋ณธ์ธ์ผ๋ก ์ผ๋จ ์ด๊ธฐํ
for(int i=1; i<=v; i++){
parent[i] = i;
}
for(int i=0; i<e; i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
pq.add(new Node(a,b,c));
}
int sum=0;
while(!pq.isEmpty()){
Node now = pq.poll();
// ๋ง์ฝ ์ฌ์ดํด์ ํ์ฑํ๋ค๋ฉด (๋ถ๋ชจ๊ฐ ๊ฐ๋ค๋ฉด) ๋์ด๊ฐ๊ธฐ
if(find(now.from)==find(now.to)) continue;
// ํ์ฑํ์ง ์๋๋ค๋ฉด ์ ํ
sum+=now.weight;
union(now.from, now.to);
}
System.out.println(sum);
}
}
๐ฉ๐ป ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ ์ฝ๋ ๊ตฌํ
public class BOJ_1197_์ต์์คํจ๋ํธ๋ฆฌ {
static boolean[] visit;
static int v,e;
static ArrayList<Node>[] list;
static class Node implements Comparable<Node>{
int from, to, weight;
public Node(int from, int to, int weight){
this.from = from;
this.to = to;
this.weight = weight;
}
public int compareTo(Node n){
return this.weight-n.weight;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
list = new ArrayList[v+1];
visit = new boolean[v+1];
for(int i=1; i<=v; i++){
list[i] = new ArrayList<>();
}
for(int i=0; i<e; i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
list[a].add(new Node(a, b, c));
list[b].add(new Node(a, b, c));
}
PriorityQueue<Node> pq = new PriorityQueue<>(); // ์ฐ์ ์์ ํ (์ต์ ํ) ์ฌ์ฉ
pq.add(new Node(0, 1, 0));
int ans=0;
while(!pq.isEmpty()){
Node node = pq.poll();
if(visit[node.b]) continue;
visit[node.b] = true;
ans+=node.weight;
// ํ์ฌ ์ ์ ์ ์ฐ๊ฒฐ๋ ๋ค๋ฅธ ์ ์ ๋ค ํ์
for(Node next : list[node.b]){
if(!visit[next.b]) pq.add(next);
}
}
System.out.println(ans);
}
}
'์๊ณ ๋ฆฌ์ฆ > ๐๏ธ ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DP - Knapsack (๋ฐฐ๋ญ๋ฌธ์ ) (1) | 2024.02.05 |
---|---|
LIS (์ต์ฅ์ฆ๊ฐ๋ถ๋ถ์์ด) (1) | 2023.12.07 |
Bellman-Ford (๋ฒจ๋ง-ํฌ๋) (0) | 2023.09.08 |
LCA (์ต์๊ณตํต์กฐ์) (0) | 2023.09.04 |
Binary Search (์ด๋ถํ์) (0) | 2023.09.01 |