728x90
1197번: 최소 스패닝 트리
첫째 줄에 정점의 개수 V(1 ≤ V ≤ 10,000)와 간선의 개수 E(1 ≤ E ≤ 100,000)가 주어진다. 다음 E개의 줄에는 각 간선에 대한 정보를 나타내는 세 정수 A, B, C가 주어진다. 이는 A번 정점과 B번 정점이
www.acmicpc.net
최소 신장 트리(Minimum Spanning Tree)를 구하는 문제로 크루스칼 알고리즘과 프림 알고리즘으로 구현해보았다.
크루스칼 알고리즘 : 최소 신장 트리(MST)를 찾는 알고리즘
신장 트리란 그래프에서 모든 노드를 포함하면서 사이클이 존재하지 않는 부분 그래프를 의미한다. 모든 노드가 포함되어 서로 연결되면서 사이클이 존재하지 않는다는 조건은 트리의 조건이
gangintheremark.tistory.com
프림 알고리즘 : 최소 신장 트리(MST)를 찾는 알고리즘
프림 알고리즘 최소 신장 트리(MST)를 찾기 위한 알고리즘이다. 프림 알고리즘은 주어진 그래프에서 시작 정점을 선택하고 해당 정점을 기준으로 MST를 구축해나가는 방식으로 동작한다. 1. 시작
gangintheremark.tistory.com
크루스칼
- 568ms
import java.io.*;
import java.util.*;
public class Main {
static int v, e;
static int[] parent;
static long result;
static List<Edge> graph = new ArrayList<>();
static class Edge implements Comparable<Edge> {
int v;
int w;
int cost;
public Edge(int v, int w, int cost) {
this.v = v;
this.w = w;
this.cost = cost;
}
@Override
public int compareTo(Edge o) {
if (this.cost < o.cost)
return -1;
return 1;
}
}
static int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
static void union(int a, int b) {
a = find(a);
b = find(b);
if (a < b)
parent[b] = parent[a];
else
parent[a] = parent[b];
}
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());
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 v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
graph.add(new Edge(v, w, c));
}
Collections.sort(graph);
for (int i = 0; i < graph.size(); i++) {
Edge e = graph.get(i);
if(find(e.v) != find(e.w)) {
union(e.v, e.w);
result += e.cost;
}
}
System.out.println(result);
}
}
프림
- 612ms
import java.io.*;
import java.util.*;
public class Main {
static int v, e;
static long result;
static List<ArrayList<Edge>> graph = new ArrayList<>();
static boolean[] visited;
static class Edge implements Comparable<Edge> {
int v;
int cost;
public Edge(int v, int cost) {
this.v = v;
this.cost = cost;
}
@Override
public int compareTo(Edge o) {
if (this.cost < o.cost)
return -1;
return 1;
}
}
static void Prim() {
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.offer(new Edge(1, 0));
while (!pq.isEmpty()) {
Edge edge = pq.poll();
if (visited[edge.v])
continue;
visited[edge.v] = true;
result += edge.cost;
for (Edge e : graph.get(edge.v))
if (!visited[e.v])
pq.offer(e);
}
}
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());
visited = new boolean[v + 1];
for (int i = 0; i <= v; i++)
graph.add(new ArrayList<>());
for (int i = 0; i < e; i++) {
st = new StringTokenizer(br.readLine());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
graph.get(v).add(new Edge(w, c));
graph.get(w).add(new Edge(v, c));
}
Prim();
System.out.println(result);
}
}
728x90
'Algorithm > Problem Solving' 카테고리의 다른 글
[BOJ/GoldIV] 1504. 특정한 최단 경로 (3) | 2024.03.05 |
---|---|
[BOJ/GoldV] 1916. 최소비용 구하기 - JAVA 자바 (0) | 2024.03.05 |
[BOJ/GoldIV] 11657. 타임머신 - JAVA 자바 (0) | 2024.03.05 |
[BOJ/GoldIV] 17471. 게리맨더링 - JAVA 자바 (0) | 2024.03.03 |
[BOJ\GoldIV] 1753. 최단 경로 - JAVA 자바 (0) | 2024.03.03 |