π [λ°±μ€μκ³ λ¦¬μ¦ νμ΄] Q.9370 λ―ΈνμΈ λμ°©μ§ λ¬Έμ νμ΄ - java
π λ¬Έμ
https://www.acmicpc.net/problem/9370
(μ·¨μ΅)B100 μμ, μλν μ·μ°¨λ¦Όμ ν μμ»€μ€ μμ κ° ν μμ΄ ν λμμ 거리λ€μ μ΄λνκ³ μλ€. λμ μ무λ κ·Έλ€μ΄ μ΄λλ‘ κ°κ³ μλμ§ μμλ΄λ κ²μ΄λ€.
μ°λ¦¬κ° μμλΈ κ²μ κ·Έλ€μ΄ sμ§μ μμ μΆλ°νλ€λ κ², κ·Έλ¦¬κ³ λͺ©μ μ§ νλ³΄λ€ μ€ νλκ° κ·Έλ€μ λͺ©μ μ§λΌλ κ²μ΄λ€. κ·Έλ€μ΄ κΈν μν©μ΄κΈ° λλ¬Έμ λͺ©μ μ§κΉμ§ μ°ννμ§ μκ³ μ΅λ¨κ±°λ¦¬λ‘ κ° κ²μ΄λΌ νμ νλ€. μ΄μμ΄λ€. (μ·¨μ΅)
μ΄ν΄! (μλν μ·μ°¨λ¦Όμ νμμ§λ λͺ¨λ₯Ό) λμ€κ° μ΄λμλ 보μ΄μ§ μλλ€. λ€ννλ λΉμ μ νκ°μ΄ κ°λ§νΌ λ°μ΄λλ€. μ΄ νκ°μΌλ‘ κ·Έλ€μ΄ gμ h κ΅μ°¨λ‘ μ¬μ΄μ μλ λλ‘λ₯Ό μ§λκ°λ€λ κ²μ μμλλ€.
μ΄ λμ€λ λ체 μ΄λλ‘ κ°κ³ μλ κ²μΌκΉ?
μμ μ λ ₯μ λ λ²μ§Έ μΌμ΄μ€λ₯Ό μκ°νν κ²μ΄λ€. μ΄ λμ€λ νμ μμμ λ κ²μ μ μ€ νλλ‘ κ°κ³ μκ³ μ μ μΌλ‘ νμλ λλ‘μμ λμλ₯Ό 맑μλ€. λ°λΌμ κ·Έλ€μ 6μΌλ‘ ν₯νκ³ μλ€.
π μ κ·Όλ²
BOJ Q.9370 λ―ΈνμΈ λμ°©μ§ λ¬Έμ μ λλ€.
μλν μ·μ°¨λ¦Όμ ν μμ»€μ€ μμ κ° ν μμ΄ ν λμμ 거리λ€μ μ΄λνκ³ μμ λ κ·Έ λμ€κ° μ΄λλ‘ κ°κ³ μλ κ²μΈμ§ λͺ©μ μ§λ₯Ό μ°ΎμμΌ νλ λ¬Έμ μ λλ€.
μΆλ° μ§μ μΌλ‘ sκ° μ£Όμ΄μ§κ³ λͺ©μ μ§ ν보λ€μ΄ μ£Όμ΄μ§λλ€.
s -> λͺ©μ μ§ νλ³΄λ€ μ μ΄ν΄λ³΄λ©΄ λλ λ¬Έμ μ λλ€.
ν μ§μ μμ λͺ©μ μ§κΉμ§μ μ΅λ¨ 거리λ₯Ό ꡬν΄μΌνλ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ΄μ©ν΄ νμ΄κ° κ°λ₯ν©λλ€.
λ¨, μ‘°κ±΄μ΄ μμ΅λλ€. λ¬Έμ μ μ£Όμ΄μ§ κ² μ²λΌ gμ h μ¬μ΄μ λλ‘λ₯Ό λ°λμ μ§λκ°μΌ ν©λλ€.
gμ hλ₯Ό λ°λμ μ§λκ°λ κ²½λ‘λ€μ λ§λ€μ΄μ£Όλ©΄ νμ΄κ° κ°λ₯ν©λλ€.
μ¦,
- s -> g -> h -> λͺ©μ μ§
- s -> h -> g -> λͺ©μ μ§
μ΄ λ κ²½λ‘ μ€ μ΅λ¨ κ±°λ¦¬μΈ κ²μ ꡬνλ©΄ λ©λλ€.
int min = Math.min(dijkstra(s, g) + dijkstra(g, h) + dijkstra(h, destination), dijkstra(s, h) + dijkstra(h, g) + dijkstra(g, destination));
λν! λͺ©μ μ§ νλ³΄λ€ μ€ λΆκ°λ₯ν κ²½μ°λ€μ μ μΈν΄μΌν©λλ€.
λ¬Έμ λ₯Ό μ μ΄ν΄λ΄μΌνμ΅λλ€.
λ€μ΄ κΈν μν©μ΄κΈ° λλ¬Έμ λͺ©μ μ§κΉμ§ μ°ννμ§ μκ³ μ΅λ¨κ±°λ¦¬λ‘ κ° κ²μ΄λΌ νμ νλ€. μ΄μμ΄λ€. (μ·¨μ΅)
μμ λ΄μ©κ³Ό κ°μ΄ μ΄ λμ€λ μ λ λͺ©μ μ§κΉμ§ μ°ννμ§ μκ³ μ΅λ¨κ±°λ¦¬λ‘ μμ§μΈλ€λ κ² μ λλ€.
λͺ©μ μ§ νλ³΄λ€ μ€ λΆκ°λ₯ν κ²½μ°λ μ°ννλ κ²½μ°μ λλ€.
κ·Έλ¬λ―λ‘ μ΅μ’ μ μΌλ‘ g,h μ¬μ΄μ λλ‘λ₯Ό λ°λμ μ§λκ°λ κ²½λ‘μ΄λ©΄μ
s->λͺ©μ μ§κΉμ§μ μ΅λ¨κ²½λ‘μΈμ§λ₯Ό νμΈνλ©΄ λ΅μ ꡬν μ μμ΅λλ€.
int min = Math.min(dijkstra(s, g) + dijkstra(g, h) + dijkstra(h, destination), dijkstra(s, h) + dijkstra(h, g) + dijkstra(g, destination));
int shortestPath = dijkstra(s, destination);
if (shortestPath == min) {
answerQueue.add(destination);
}
π» μ½λ
package problem.shortest.Q9370;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
public class Main_9370 {
static class Node implements Comparable<Node> {
int index;
int distance;
public Node(int index, int distance) {
this.index = index;
this.distance = distance;
}
@Override
public int compareTo(Node target) {
// μμ 거리 λΉμ©μ΄ λ¨Όμ μ€λλ‘
return this.distance - target.distance;
}
}
static final int INF = 100000000;
static int start, end;
static int T, n, m, t;
static int s, g, h;
static List<List<Node>> adList;
static int[] distance;
static boolean[] visited;
static PriorityQueue<Node> priorityQueue;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
String input[] = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
t = Integer.parseInt(input[2]);
input = br.readLine().split(" ");
s = Integer.parseInt(input[0]);
g = Integer.parseInt(input[1]);
h = Integer.parseInt(input[2]);
adList = new ArrayList<>();
// μΈλ±μ€λ₯Ό 1λΆν° νκΈ° μν΄μ ν κ°λ₯Ό λ£μ΄λ
adList.add(new <Node>ArrayList());
for (int i = 1; i <= n; i++) {
adList.add(new <Node>ArrayList());
}
for (int i = 0; i < m; i++) {
String[] temp = br.readLine().split(" ");
int u = Integer.parseInt(temp[0]);
int v = Integer.parseInt(temp[1]);
int cost = Integer.parseInt(temp[2]);
adList.get(u).add(new Node(v, cost));
adList.get(v).add(new Node(u, cost));
}
PriorityQueue<Integer> answerQueue = new PriorityQueue<>();
for (int i = 0; i < t; i++) {
int destination = Integer.parseInt(br.readLine());
int min = Math.min(dijkstra(s, g) + dijkstra(g, h) + dijkstra(h, destination), dijkstra(s, h) + dijkstra(h, g) + dijkstra(g, destination));
int shortestPath = dijkstra(s, destination);
if (shortestPath == min) {
answerQueue.add(destination);
}
}
while (!answerQueue.isEmpty()) {
sb.append(answerQueue.poll() + " ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
private static int dijkstra(int start, int end) {
priorityQueue = new PriorityQueue<>();
visited = new boolean[n + 1];
distance = new int[n + 1];
Arrays.fill(distance, INF);
distance[start] = 0;
priorityQueue.add(new Node(start, 0));
while (!priorityQueue.isEmpty()) {
Node current = priorityQueue.poll();
if (visited[current.index]) {
continue;
}
visited[current.index] = true;
// μ°κ²°λ μ μ λ€μ νμΈ
for (Node destination : adList.get(current.index)) {
//λͺ©μ μ§ μ§ν , νμ¬κΉμ§ 거리 + νμ¬μμ λͺ©μ μ§ κ±°λ¦¬
if (distance[destination.index] > distance[current.index] + destination.distance) {
// μ΅μ 거리 λΉμ© κ°±μ
distance[destination.index] = distance[current.index] + destination.distance;
priorityQueue.add(new Node(destination.index, distance[destination.index]));
}
}
}
return distance[end];
}
}