Skip to content

Commit

Permalink
Create 1197_최소 스패닝 트리.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-HinD authored Aug 24, 2023
1 parent e3b48d9 commit 4137167
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions 1197_최소 스패닝 트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

static class Edge implements Comparable<Edge>{
int from, to;
int weight;

public Edge(int from, int to, int weight) {
super();
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

static int V, E;
static int[] parents;

static void make() { //초기화 메서드
parents = new int[V];
for (int i = 0; i < V; i++) {
parents[i] = i;
}
}

static int find(int a) { //root노드 찾아주는 메서드
if(parents[a] == a) return a;
return parents[a] = find(parents[a]);
}

static boolean union(int a, int b) { //b의 root 변경 메서드
int aRoot = find(a);
int bRoot = find(b);
if(aRoot == bRoot) return false;
parents[bRoot] = aRoot;
return true;
}

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()) + 1; //정점 1부터 시작
E = Integer.parseInt(st.nextToken());

PriorityQueue<Edge> pq = new PriorityQueue<>(); //우선순위 큐 사용

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 weight = Integer.parseInt(st.nextToken());
pq.add(new Edge(from, to, weight));
}

//V개의 정점으로 make set 작업
make();

int result = 0; //MST 비용
int cnt = 0; //연결된 간선 개수

for(int i=0; i<pq.size(); i++) {
Edge e = pq.poll();
if(union(e.from, e.to)) { //사이클 형성 확인
result += e.weight; //값 합산
if(++cnt == V-1) break; //스패닝 트리 완성조건
}
i--;
}
System.out.println(result);
}
}

0 comments on commit 4137167

Please sign in to comment.