π [λ°±μ€μκ³ λ¦¬μ¦ νμ΄] Q.1504 νΉμ ν μ΅λ¨ κ²½λ‘
π λ¬Έμ
https://www.acmicpc.net/problem/1504
λ°©ν₯μ±μ΄ μλ κ·Έλνκ° μ£Όμ΄μ§λ€. μΈμ€μ΄λ 1λ² μ μ μμ Nλ² μ μ μΌλ‘ μ΅λ¨ κ±°λ¦¬λ‘ μ΄λνλ €κ³ νλ€.
λν μΈμ€μ΄λ λ κ°μ§ 쑰건μ λ§μ‘±νλ©΄μ μ΄λνλ νΉμ ν μ΅λ¨ κ²½λ‘λ₯Ό ꡬνκ³ μΆμλ°,
κ·Έκ²μ λ°λ‘ μμλ‘ μ£Όμ΄μ§ λ μ μ μ λ°λμ ν΅κ³Όν΄μΌ νλ€λ κ²μ΄λ€.
μΈμ€μ΄λ νλ² μ΄λνλ μ μ μ λ¬Όλ‘ , νλ² μ΄λνλ κ°μ λ λ€μ μ΄λν μ μλ€.
νμ§λ§ λ°λμ μ΅λ¨ κ²½λ‘λ‘ μ΄λν΄μΌ νλ€λ μ¬μ€μ μ£ΌμνλΌ. 1λ² μ μ μμ Nλ² μ μ μΌλ‘ μ΄λν λ,
μ£Όμ΄μ§ λ μ μ μ λ°λμ κ±°μΉλ©΄μ μ΅λ¨ κ²½λ‘λ‘ μ΄λνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
π μ κ·Όλ²
BOJ Q.1504 νΉμ ν μ΅λ¨ κ²½λ‘ λ¬Έμ μ λλ€.
κΈ°λ³Έ λ°νμ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μΌλ‘ νμ΄κ° κ°λ₯ν©λλ€.
μ¬κΈ°μ λ€μ λ€λ₯Έ κ²μ μμλ‘ μ£Όμ΄μ§ λ μ μ μ λ°λμ ν΅κ³Όν΄μΌ νλ€λ κ² μ λλ€.
λμ°©μ§κΉμ§ μ μ 1 , μ μ 2 λ₯Ό λ°λμ ν΅κ³Όνλ κ²½μ°λ λ€μκ³Ό κ°μ΅λλ€.
μμ -> μ μ 1 -> μ μ 2 -> λ μμ -> μ μ 2 -> μ μ 1 -> λ
μ΄λ κ² λκ°μ§ κ²½μ°μ€μ λ μ΅λ¨ κ²½λ‘λ₯Ό μ°ΎμΌλ©΄ νμ΄κ° κ°λ₯ν©λλ€.
solve ν¨μ => λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ΄μ©ν΄ μ£Όμ΄μ§ μμμ λΆν° λκΉμ§μ μ΅λ¨κ²½λ‘λ₯Ό ꡬνλ ν¨μ solve ν¨μλ₯Ό μ΄μ©ν΄ μλμ κ°μ΄ μμ -> μ μ 1 -> μ μ 2 -> λ μμ -> μ μ 2 -> μ μ 1 -> λ
λκ°μ§ μ€ λ 짧μ κ²½λ‘κ° νΉμ ν μ΅λ¨ κ²½λ‘μ λλ€.
answer[0] += solve(1,first)+solve(first,second)+solve(second,N);
answer[1] += solve(1,second)+solve(second,first)+solve(first,N);
π» μ½λ
package problem.shortest.Q1504;
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_1504 {
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 N, M, start,end;
static List<List<Node>> adList;
static int[] distance;
static boolean[] visited;
static PriorityQueue<Node> priorityQueue;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int answer[] = {0,0};
String input[] = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
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));
}
input = br.readLine().split(" ");
int first = Integer.parseInt(input[0]);
int second = Integer.parseInt(input[1]);
answer[0] += solve(1,first)+solve(first,second)+solve(second,N);
answer[1] += solve(1,second)+solve(second,first)+solve(first,N);
int shortestPathCost = Integer.min(answer[0],answer[1]);
if(shortestPathCost>=INF)
System.out.println(-1);
else
System.out.println(shortestPathCost);
}
private static int solve(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];
}
}
}