반응형
- 문제 : www.acmicpc.net/problem/1922
1922번: 네트워크 연결
이 경우에 1-3, 2-3, 3-4, 4-5, 4-6을 연결하면 주어진 output이 나오게 된다.
www.acmicpc.net
MST 문제
This file contains 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 java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.InputStreamReader; | |
import java.util.*; | |
public class 네트워트연결_1922 { | |
private static int N, M; | |
private static Point[] POINTS; | |
private static int[] P; | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
StringTokenizer st = null; | |
N = Integer.parseInt(br.readLine()); | |
M = Integer.parseInt(br.readLine()); | |
POINTS = new Point[M]; | |
P = new int[N+1]; | |
Arrays.fill(P, -1); | |
for (int i=0; i<M; i++) { | |
st = new StringTokenizer(br.readLine()); | |
int a = Integer.parseInt(st.nextToken()); | |
int b = Integer.parseInt(st.nextToken()); | |
int c = Integer.parseInt(st.nextToken()); | |
POINTS[i] = new Point(a, b, c); | |
} | |
// System.out.println("POINTS : " + Arrays.toString(POINTS)); | |
Arrays.sort(POINTS, new Comparator<Point>() { | |
@Override | |
public int compare(Point o1, Point o2) { | |
return o1.value - o2.value; | |
} | |
}); | |
// System.out.println("POINTS : " + Arrays.toString(POINTS)); | |
int answer = 0; | |
int count = 0; | |
for (Point point : POINTS) { | |
if (count >= N-1) { | |
break; | |
} | |
int a = find(point.from); | |
int b = find(point.next); | |
if (a != b) { | |
answer += point.value; | |
union(point.from, point.next); | |
count++; | |
} | |
} | |
System.out.println(answer); | |
} | |
private static int find(int a) { | |
if (P[a] < 0) { | |
return a; | |
} | |
return P[a] = find(P[a]); | |
} | |
private static void union(int a, int b) { | |
a = find(a); | |
b = find(b); | |
if (a == b) { | |
return; | |
} | |
P[a] += P[b]; | |
P[b] = a; | |
} | |
static class Point { | |
int from, next, value; | |
public Point(int from, int next, int value) { | |
this.from = from; | |
this.next = next; | |
this.value = value; | |
} | |
@Override | |
public String toString() { | |
return "Point{" + | |
"from=" + from + | |
", next=" + next + | |
", value=" + value + | |
'}'; | |
} | |
} | |
} |
반응형
'개발' 카테고리의 다른 글
EHCache (0) | 2021.02.18 |
---|---|
Git 명령어에 익숙해지자 (자주쓰는 명령어) (0) | 2021.02.03 |
[백준 1948] 임계경로 (0) | 2021.01.17 |
[백준 2529] 부등호 (0) | 2021.01.16 |
[백준 2623] 음악프로그램 (0) | 2021.01.16 |