๋ฐ์ํ
[BOJ] 1197๋ฒ ์ต์ ์คํจ๋ ํธ๋ฆฌ
https://www.acmicpc.net/problem/1197
๋ฌธ์ ์ ๊ทผ
๋ชจ๋ ์ ์ ์ ์ต์ ๊ฐ์ผ๋ก ์ํํ ์ ์๋ ํธ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
๋ํ์ ์ผ๋ก Kruskal๊ณผ Prim ์๊ณ ๋ฆฌ์ฆ์ด ์๊ธฐ ๋๋ฌธ์ ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ๊ตฌํํ์๋ค.
Code
Kruskal ์๊ณ ๋ฆฌ์ฆ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
class Edge implements Comparable<Edge> {
int from;
int to;
int cost;
public Edge(int from, int to, int cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Edge o) {
return cost - o.cost;
}
}
public class BOJ1197_kruskal {
static int V;
static int E;
static Edge[] pq;
static int[] root;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
root = new int[V + 1];
pq = new Edge[E];
for(int i = 0; i <= V; i++){
root[i] = i;
}
for(int i = 0; i < E; i++){
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
pq[i] = new Edge(from, to, cost);
}
System.out.println(kruskal());
}
public static int kruskal(){
int sum = 0;
int cnt = 0;
Arrays.sort(pq);
for (Edge edge : pq) {
if(union(edge.from, edge.to)){
sum += edge.cost;
if(++cnt == V - 1) return sum;
}
}
return sum;
}
public static boolean union(int a, int b){
a = find(a);
b = find(b);
if(a == b) return false;
root[a] = b;
return true;
}
public static int find(int a){
if(root[a] == a) return a;
else {
return root[a] = find(root[a]);
}
}
}
Prim ์๊ณ ๋ฆฌ์ฆ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class BOJ1197_prime {
static class Node implements Comparable<Node>{
int to,cost;
public Node(int to, int cost) {
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return cost - o.cost;
}
}
static int V,E;
static ArrayList<Node>[] adjList; // ์ธ์ ๋ฆฌ์คํธ
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
adjList = new ArrayList[V + 1];
for(int i = 1; i<=V; i++){
adjList[i] = new ArrayList<>();
}
visited = new boolean[V + 1];
for(int i = 0; i<E; i++){
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
adjList[from].add(new Node(to, cost));
adjList[to].add(new Node(from, cost));
}
System.out.println(prim());
}
public static int prim(){
int sum = 0;
int cnt = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(1, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if(visited[cur.to]) continue;
sum += cur.cost;
visited[cur.to] = true;
if(++cnt == V) return sum;
for(Node n : adjList[cur.to]){
if(visited[n.to]) continue;
pq.add(n);
}
}
return sum;
}
}
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Java][๋ฐฑ์ค] 14226๋ฒ ์ด๋ชจํฐ์ฝ (0) | 2022.06.05 |
---|---|
[Algorithm/Java][๋ฐฑ์ค] 2252๋ฒ ์ค ์ธ์ฐ๊ธฐ (0) | 2022.05.24 |
[Algorithm/Java][๋ฐฑ์ค] 1916๋ฒ ์ต์๋น์ฉ ๊ตฌํ๊ธฐ (0) | 2022.05.20 |
[Algorithm/Java][๋ฐฑ์ค] 1806๋ฒ ๋ถ๋ถํฉ (0) | 2022.05.18 |
[Algorithm/Java][๋ฐฑ์ค] 1700๋ฒ ๋ฉํฐํญ ์ค์ผ์ค๋ง (0) | 2022.05.17 |