[BOJ] 2887 νμ± ν°λ JAVA
[λ¬Έμ ]
https://www.acmicpc.net/problem/2887
2887λ²: νμ± ν°λ
첫째 μ€μ νμ±μ κ°μ Nμ΄ μ£Όμ΄μ§λ€. (1 ≤ N ≤ 100,000) λ€μ Nκ° μ€μλ κ° νμ±μ x, y, zμ’νκ° μ£Όμ΄μ§λ€. μ’νλ -109λ³΄λ€ ν¬κ±°λ κ°κ³ , 109λ³΄λ€ μκ±°λ κ°μ μ μμ΄λ€. ν μμΉμ νμ±μ΄ λ κ° μ΄
www.acmicpc.net
λλ 2040λ
, μ΄λ―Όνμ μ°μ£Όμ μμ λ§μ μκ΅μ λ§λ€μλ€. μκ΅μ Nκ°μ νμ±μΌλ‘ μ΄λ£¨μ΄μ Έ μλ€. λ―Όνμ΄λ μ΄ νμ±μ ν¨μ¨μ μΌλ‘ μ§λ°°νκΈ° μν΄μ νμ±μ μ°κ²°νλ ν°λμ λ§λ€λ €κ³ νλ€.
νμ±μ 3μ°¨μ μ’νμμ ν μ μΌλ‘ μκ°νλ©΄ λλ€. λ νμ± A(xA, yA, zA)μ B(xB, yB, zB)λ₯Ό ν°λλ‘ μ°κ²°ν λ λλ λΉμ©μ min(|xA-xB|, |yA-yB|, |zA-zB|)μ΄λ€.
λ―Όνμ΄λ ν°λμ μ΄ N-1κ° κ±΄μ€ν΄μ λͺ¨λ νμ±μ΄ μλ‘ μ°κ²°λκ² νλ €κ³ νλ€. μ΄λ, λͺ¨λ νμ±μ ν°λλ‘ μ°κ²°νλλ° νμν μ΅μ λΉμ©μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
[μ
λ ₯]
첫째 μ€μ νμ±μ κ°μ Nμ΄ μ£Όμ΄μ§λ€. (1 ≤ N ≤ 100,000) λ€μ Nκ° μ€μλ κ° νμ±μ x, y, zμ’νκ° μ£Όμ΄μ§λ€. μ’νλ -109λ³΄λ€ ν¬κ±°λ κ°κ³ , 109λ³΄λ€ μκ±°λ κ°μ μ μμ΄λ€. ν μμΉμ νμ±μ΄ λ κ° μ΄μ μλ κ²½μ°λ μλ€.
[μΆλ ₯]
첫째 μ€μ λͺ¨λ νμ±μ ν°λλ‘ μ°κ²°νλλ° νμν μ΅μ λΉμ©μ μΆλ ₯νλ€.
[Solution 1]
- μ΅μμ μ₯νΈλ¦¬ λ¬Έμ
- ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦ μ¬μ©
- κ°μ μ΅μκ°μ ꡬνλ κ³Όμ (line 57~70)μμ λ©λͺ¨λ¦¬ or μκ°μ΄κ³Ό
package boj2887_νμ±ν°λ;
/* μ΅μμ μ₯νΈλ¦¬ λ¬Έμ
* ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦
* -> λ©λͺ¨λ¦¬μ΄κ³Ό
* weightμ minκ° κ΅¬νλ λΆλΆμμ 100,000*99999/2*12(byte) > 128MB
* */
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 start, end, weight;
public Edge(int start, int end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
return weight-o.weight;
}
}
static int n;
static int[][] space;
static PriorityQueue<Edge> pq = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
space = new int[n][3];
int [] parent = new int[n];
int answer = 0;
int weight = 0;
//iνμ±μ μ’νκ° input
for(int i=0; i<n; ++i) {
st = new StringTokenizer(br.readLine());
space[i][0] = Integer.parseInt(st.nextToken()); //x
space[i][1] = Integer.parseInt(st.nextToken()); //y
space[i][2] = Integer.parseInt(st.nextToken()); //z
}
//Aνμ±-Bνμ± μ°κ²°μ λΉμ© : min(|xA-xB|, |yA-yB|, |zA-zB|)
//i-j = j-i μ΄λ―λ‘ jλ iλΆν° μμ(λλ² ν νμx)
for(int i=0; i<n; ++i) {
for(int j=i; j<n; ++j) {
if(i == j) continue;
//x
weight = Math.abs(space[i][0]-space[j][0]);
//x, y
weight = Math.min(weight, Math.abs(space[i][1]-space[j][1]));
//x, y, z
weight = Math.min(weight, Math.abs(space[i][2]-space[j][2]));
pq.add(new Edge(i, j, weight));
}
}
//parentλ₯Ό μμ μΌλ‘ μ΄κΈ°ν
for(int i=0; i<n; ++i) {
parent[i] = i;
}
//κ°μ€μΉ μμκ²(μμͺ½idx)λΆν° μ ννλ©΄μ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦ μ€ν
int len = pq.size();
for(int i=0; i<len; ++i) {
Edge node = pq.poll();
//μ¬μ΄ν΄μ΄ λ°μνμ§ μλ κ²½μ°(μμλ
Έλμ λμ°©λ
Έλμ 루νΈλ
Έλκ° κ°μ§μμΌλ©΄)
//μ μ§ν©μ ν¬ν¨(union ν¨μ μ€ν), λ
Έλ(ν°λ) μ°κ²°
if(ckCycle(parent, node.start) != ckCycle(parent, node.end)) {
union(parent, node.start, node.end);
answer += node.weight;
}
}
System.out.println(answer);
}
//xκ° μν μ§ν© μ°ΎκΈ°(μ¬μ΄ν΄ λ°μ νμΈ), 루νΈλ
Έλ λ°ν
static int ckCycle(int[] parent, int node) {
//parent[node]κ° νμ¬ λ
Έλλ λ€λ₯΄λ©΄ 루νΈλ
Έλκ° λ°λ‘ μμΌλ―λ‘
//루νΈλ
Έλ μ°Ύμμ μ¬λΌκ°λ μ¬κ·
if(parent[node] != node) {
parent[node] = ckCycle(parent, parent[node]);
}
return parent[node];
}
//start, end μν μ§ν© ν©μΉκΈ°
static void union(int[] parent, int start, int end) {
//startμ endλ
Έλμ 루νΈλ
Έλ μ°Ύμμ μ μ₯
start = ckCycle(parent, start);
end = ckCycle(parent, end);
//λ μμ μλ(μ«μκ° μμ) λ
Έλκ° λ£¨νΈλ
Έλκ° λ¨
if(start < end) parent[end] = start;
else parent[start] = end;
}
}
[Solution 2]
- ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦ κ·Έλλ‘ μ¬μ©
- minκ°μ ꡬνμ§ μκ³ μ°μ μμ νλ₯Ό μ΄μ©
- x, y, z κ°μ μ κ°μ€μΉλ₯Ό κ°μ ꡬν ν
- κ°μ€μΉκ° μμ μμΌλ‘ μ λ ¬ ν λ
Έλλ₯Ό μ°κ²°νλ©΄ μκ°, λ©λͺ¨λ¦¬ μ μ½!
package boj2887_νμ±ν°λ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main2 {
static class Edge implements Comparable<Edge>{
int start, end, weight;
public Edge(int start, int end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}
//κ°μ€μΉ μ€λ¦μ°¨μ μ λ ¬
@Override
public int compareTo(Edge o) {
return weight-o.weight;
}
}
static class Node implements Comparable<Node> {
int pos, num; //μ’ν, λ
Έλλ²νΈ
public Node(int pos, int num) {
this.pos = pos;
this.num = num;
}
//μ’ν μ€λ¦μ°¨μ, μ’νκ° κ°μΌλ©΄ λ
Έλμ
@Override
public int compareTo(Node o) {
if(pos == o.pos) return num - o.num;
return pos - o.pos;
}
}
static int n;
static PriorityQueue<Edge> pq = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
int [] parent = new int[n];
//μ°μ μμ νλ₯Ό μ¬μ©ν΄μ μ λ ¬ ν΄μ£Όμ§ μμλλ¨
PriorityQueue<Node> x = new PriorityQueue<>();
PriorityQueue<Node> y = new PriorityQueue<>();
PriorityQueue<Node> z = new PriorityQueue<>();
int answer = 0;
//iνμ±μ μ’νκ° input
for(int i=0; i<n; ++i) {
st = new StringTokenizer(br.readLine());
x.add(new Node(Integer.parseInt(st.nextToken()), i));
y.add(new Node(Integer.parseInt(st.nextToken()), i));
z.add(new Node(Integer.parseInt(st.nextToken()), i));
}
//Aνμ±-Bνμ± μ°κ²°λΉμ© : x,y,zμ’ν κ³μ°κ°μ μ λκ° μ€ μ΅μκ°μ΄μ§λ§ μ΅μκ°μ ꡬνλ κ³Όμ μ νμμμ.
//μ€λ¦μ°¨μ ν λΉμ©μ΄ μμ κ°μ (ν°λ)λΆν° μ°κ²°ν΄μ μ΅μκ°μ κ³μ°νλ κ³Όμ μμ΄λ γ±γ
//|xA-xB|
for(int i=0; i<n-1; ++i) {
Node A = x.poll();
Node B = x.peek();
pq.add(new Edge(A.num, B.num, Math.abs(A.pos - B.pos)));
}
//|yA-yB|
for(int i=0; i<n-1; ++i) {
Node A = y.poll();
Node B = y.peek();
pq.add(new Edge(A.num, B.num, Math.abs(A.pos - B.pos)));
}
//|zA-zB|
for(int i=0; i<n-1; ++i) {
Node A = z.poll();
Node B = z.peek();
pq.add(new Edge(A.num, B.num, Math.abs(A.pos - B.pos)));
}
//parentλ₯Ό μμ μΌλ‘ μ΄κΈ°ν
for(int i=0; i<n; ++i) {
parent[i] = i;
}
//κ°μ€μΉ μμκ²(μμͺ½idx)λΆν° μ ννλ©΄μ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦ μ€ν
int len = pq.size();
for(int i=0; i<len; ++i) {
Edge node = pq.poll();
//μ¬μ΄ν΄μ΄ λ°μνμ§ μλ κ²½μ°(μμλ
Έλμ λμ°©λ
Έλμ 루νΈλ
Έλκ° κ°μ§μμΌλ©΄)
//μ μ§ν©μ ν¬ν¨(union ν¨μ μ€ν), λ
Έλ(ν°λ) μ°κ²°
if(ckCycle(parent, node.start) != ckCycle(parent, node.end)) {
union(parent, node.start, node.end);
answer += node.weight;
}
}
System.out.println(answer);
}
//xκ° μν μ§ν© μ°ΎκΈ°(μ¬μ΄ν΄ λ°μ νμΈ), 루νΈλ
Έλ λ°ν
static int ckCycle(int[] parent, int node) {
//parent[node]κ° νμ¬ λ
Έλλ λ€λ₯΄λ©΄ 루νΈλ
Έλκ° λ°λ‘ μμΌλ―λ‘
//루νΈλ
Έλ μ°Ύμμ μ¬λΌκ°λ μ¬κ·
if(parent[node] != node) {
parent[node] = ckCycle(parent, parent[node]);
}
return parent[node];
}
//start, end μν μ§ν© ν©μΉκΈ°
static void union(int[] parent, int start, int end) {
//startμ endλ
Έλμ 루νΈλ
Έλ μ°Ύμμ μ μ₯
start = ckCycle(parent, start);
end = ckCycle(parent, end);
//λ μμ μλ(μ«μκ° μμ) λ
Έλκ° λ£¨νΈλ
Έλκ° λ¨
if(start < end) parent[end] = start;
else parent[start] = end;
}
}