π [λ°±μ€μκ³ λ¦¬μ¦ νμ΄] Q.1238 νν° λ¬Έμ νμ΄ - java
π λ¬Έμ
https://www.acmicpc.net/problem/1238
Nκ°μ μ«μλ‘ κ΅¬λΆλ κ°κ°μ λ§μμ ν λͺ μ νμμ΄ μ΄κ³ μλ€.
μ΄λ λ μ΄ Nλͺ μ νμμ΄ X (1 β€ X β€ N)λ² λ§μμ λͺ¨μ¬μ νν°λ₯Ό λ²μ΄κΈ°λ‘ νλ€. μ΄ λ§μ μ¬μ΄μλ μ΄ Mκ°μ λ¨λ°©ν₯ λλ‘λ€μ΄ μκ³ iλ²μ§Έ κΈΈμ μ§λλλ° Ti(1 β€ Ti β€ 100)μ μκ°μ μλΉνλ€.
κ°κ°μ νμλ€μ νν°μ μ°ΈμνκΈ° μν΄ κ±Έμ΄κ°μ λ€μ κ·Έλ€μ λ§μλ‘ λμμμΌ νλ€. νμ§λ§ μ΄ νμλ€μ μλ κ²μλ¬μ μ΅λ¨ μκ°μ μ€κ³ κ°κΈ°λ₯Ό μνλ€.
μ΄ λλ‘λ€μ λ¨λ°©ν₯μ΄κΈ° λλ¬Έμ μλ§ κ·Έλ€μ΄ μ€κ³ κ°λ κΈΈμ΄ λ€λ₯Όμ§λ λͺ¨λ₯Έλ€. Nλͺ μ νμλ€ μ€ μ€κ³ κ°λλ° κ°μ₯ λ§μ μκ°μ μλΉνλ νμμ λꡬμΌμ§ ꡬνμ¬λΌ.
π μ κ·Όλ²
BOJ Q.1238 νν° λ¬Έμ μ λλ€.
Mκ°μ λ¨λ°©ν₯ λλ‘λ€μ΄ μ£Όμ΄μ§κ³ κ° λλ‘μ λν΄ μ§λκ°λ μκ°μ΄ μ£Όμ΄μ§λλ€.
μ¬κΈ°μ ꡬν΄μΌ ν κ²μ
Nλͺ μ νμμ΄ Xλ² λ§μμ λͺ¨μ¬μ νν°λ₯Ό λ²μΌ λ Nλͺ μ νμλ€ μ€ μ€κ³ κ°λλ° κ°μ₯ λ§μ μκ°μ μλΉνλ νμμ΄
λꡬμΈμ§λ₯Ό ꡬνλ κ²μ λλ€.
νλμ μ μ μΌλ‘ λΆν° λͺ¨λ μ μ κΉμ§μ μ΅λ¨κ±°λ¦¬λ₯Ό ꡬν μ μλ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ΄μ©νλ©΄ νμ΄κ° κ°λ₯ν©λλ€.
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ ν΅ν΄ λμ°© μ§μ μ Xλ²μΌλ‘ μ ν΄μ ΈμμΌλ
κ° νμμ μ§λΆν° xλ²κΉμ§ κ·Έλ¦¬κ³ λμμμΌ νλ xλ²μ§μ λΆν° μ§κΉμ§μ
μ΅λ¨ κ²½λ‘λ₯Ό ꡬνμ¬μ κ·Έ μ€ μ΅μκ°μ ꡬνλ©΄
μ λ΅μ λμΆν μ μμ΅λλ€.
π» μ½λ
package problem.shortest.Q1238;
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_1238 {
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 = 987654321;
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 = Integer.MIN_VALUE;
String input[] = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
end = 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));
}
for(int i = 1; i<=N; i++) {
int sum = solve(i,end);
sum += solve(end,i);
answer = Integer.max(sum,answer);
}
System.out.println(answer);
}
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];
}
}