Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- to display the conditions report re-run your application with 'debug' enabled
- 별자리 만들기
- 소가길을건너간이유6
- jenkins
- 호석이두마리치킨
- 14466
- 프로그래머스
- 이클립스
- 21278
- documentationpluginsbootstrapper
- 20055
- Eclipse
- 자바
- Java
- 2108_통계학
- 18222
- 2167. 2차원 배열의 합
- dockercompose
- CMD
- docker
- 날짜일수
- 백준
- 이산수학
- EC2
- Error
- SpringBoot
- 알고리즘
- 설정
- Error fetching remote repo 'origin'
- 투에모스문자열
Archives
- Today
- Total
계단을 오르듯이
[JAVA] 1647. 도시 분할 계획 본문
도시를 분할 때 2개의 도시를 만들고 연결하는 최소의 유지비용을 구하는 문제이다.
최소의 거리값을 구하는 크루스칼 알고리즘을 사용하였다.
우선 최소의 비용을 구하기 위해 가장 작은 비용으로 모든 집을 연결해야했고, 그 중 최소의 연결 비용을 위해 가장 큰 값의 연결 유지비용을 제외해 마을을 나누어야 한다.
문제에서 2개의 도시는 최소 1개 이상의 집이 존재하여야 한다고 하였으므로 각 마을의 집을 모두 최소의 비용으로 연결한 후 마지막 가장 큰 비용으로 연결되어지는 하나의 집을 제외하여 다른 마을로 만들면 그 방식이 가장 최소의 비용으로 모든 집을 연결하고, 가장 큰 비용이 드는 집의 연결을 제거해 하나의 마을로 만드는 것이 최소의 유지비용이 될 것이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package algo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B_1647_도시분할계획 {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Node[] list = new Node[M];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(in.readLine(), " ");
int A = Integer.parseInt(st.nextToken()) - 1;
int B = Integer.parseInt(st.nextToken()) - 1;
int C = Integer.parseInt(st.nextToken());
list[i] = new Node(A, B, C);
}
int[] parents = new int[N];
make(N, parents);
Arrays.sort(list);
int cnt = 0;
int result = 0;
for (int i = 0; i < M; i++) {
if (union(list[i].start, list[i].end, parents)) {
if (++cnt == N - 1)
break;
result += list[i].weight; // 맨 마지막 연결을 제외하기 위해 if문 다음에 위치
}
}
System.out.println(result);
}
private static void make(int N, int[] parents) {
for (int i = 0; i < N; i++) {
parents[i] = i;
}
}
private static int find(int a, int[] parents) {
if (a == parents[a])
return a;
return parents[a] = find(parents[a], parents);
}
private static boolean union(int a, int b, int[] parents) {
int aRoot = find(a, parents);
int bRoot = find(b, parents);
if (aRoot == bRoot)
return false;
parents[aRoot] = bRoot;
return true;
}
static class Node implements Comparable<Node> {
int start, end, weight;
public Node(int start, int end, int weight) {
super();
this.start = start;
this.end = end;
this.weight = weight;
}
@Override
public int compareTo(Node o) {
return this.weight - o.weight;
}
}
}
|
cs |
'알고리즘 > 백준_JAVA' 카테고리의 다른 글
[JAVA] 4386. 별자리 만들기 (0) | 2022.02.02 |
---|---|
[JAVA] 9663. N-Queen (0) | 2022.02.02 |
[JAVA] 18222. 투에 모스 문자열 (0) | 2022.02.01 |
[JAVA] 프로그래머스 - 카펫 (0) | 2022.01.31 |
[JAVA] 14916. 거스름돈 (0) | 2022.01.28 |