https://www.acmicpc.net/problem/4485
공부하다보니 갑자기 다익스트라가 기억이 안나 복기할겸 풀어보았다
최단 경로를 찾는 알고리즘은 크게 두가지가 있는데
근데 여기선 다익스트라만 알아보겠다
둘의 쓰임세만 보자면 폴로이드는 모든 경우의 수의 최단경로를 찾는 법이고 다익스트라는 한 노드에서 다른 모든 노드로 가는 최단 경로를 전부 찾는 법이다.
이 문제에선 다익스트라가 쓰인다

import java.io.*;
import java.util.*;
public class Main {
private static int N;
private static int[][] costs;
private static int[][] arr;
private static final int[] X_DIR = {1,0,-1,0};
private static final int[] Y_DIR = {0,1,0,-1};
public static void main(final String[] args) throws IOException {
problem(new InputStreamReader(System.in));
}
public static void problem(final InputStreamReader isr) throws IOException {
final BufferedReader br = new BufferedReader(isr);
N = Integer.parseInt(br.readLine());
int i = 1;
while(N != 0){
init(br);
System.out.println("Problem " + i + ": " + dijkstra());
N = Integer.parseInt(br.readLine());
i++;
}
init(new BufferedReader(isr));
}
private static void init(final BufferedReader br) throws IOException {
costs = new int[N][N];
arr = new int[N][N];
for (int i = 0; i < N; i++) {
final String[] line = br.readLine().split(" ");
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(line[j]);
costs[i][j] = Integer.MAX_VALUE;
}
}
}
private static int dijkstra(){
final PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2)->o1[2] - o2[2]);
pq.offer(new int[]{0,0,arr[0][0]});
costs[0][0] = arr[0][0];
while(!pq.isEmpty()){
final int[] node = pq.poll();
for (int dir = 0; dir < 4; dir++) {
final int nextX = node[0] + X_DIR[dir];
final int nextY = node[1] + Y_DIR[dir];
if (nextX > N-1 || nextX < 0 || nextY > N-1 || nextY < 0){
continue;
}
final int value = node[2] + arr[nextX][nextY];
if (node[2] + arr[nextX][nextY] < costs[nextX][nextY]){
costs[nextX][nextY] = value;
pq.offer(new int[]{nextX,nextY,value});
}
}
}
return costs[N-1][N-1];
}
}