๐ ๏ธ ๋ฌธ์ ๐ ๏ธ
๐๏ธ ์ค๋ช ๐๏ธ
์ฐ์ ์ค๋ ์ด ๋ฌธ์ ๋ฅผ ํธ๋๋ฐ ๊ฐ์ฅ ๋ง์ด ์ฝ์งํ ์๊ฐ์ ์์ ๋ฅผ ๋ง์ถ๋ ค๊ณ ๋ ธ๋ ฅํ ์๊ฐ์ด๋ค.
์ด์ ๋น์ทํ ๋ฌธ์ ๋ฅผ ํ์ด๋ณธ ๊ฒฝํ์ด ์์ด ์๋ฆฌ๋ฅผ ์๊ณ ์์๊ณ ์์ ๋ฅผ ๋ฏฟ๊ณ ๋ฐ๋ก ํ์ด์ ๋ค์ด๊ฐ๋ค.
๊ทธ๋ฌ๋ ๊ณ์ํด์ ๊ฒฝ๋ก๊ฐ ์์ ์ ๋ต๊ณผ ๋ค๋ฅด๊ฒ [1, 4, 5]๋ก ์ถ๋ ฅ์ด ๋์๋ค.
์ด๋ฐ์๋ ๋ฌธ์ ๊ฐ ํ๋ฆด ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์ง ์๊ณ ๋ด ์ฝ๋๋ง ๊ณ์ํด์ ํ์ธํ๋ค.
ํ์ง๋ง ์๋ฌด๋ฆฌ ์ค๋ฅ๋ฅผ ์ฐพ์๋ณด๋ ค๊ณ ํด๋ ๋ฐ๊ฒฌ๋์ง ์์๊ณ ์์ ๋ฅผ ์ง์ ์์ผ๋ก ํ์ด๋ณด๋ ์ค์ ๊ฒฝ๋ก๊ฐ [1, 4, 5]๊ฐ ๋ง์๋ค.
์ด๋ฒ์ ๋๋ ๊ฒ์ ์๋ฌด๋ฆฌ ์๋ฆฌ๋ฅผ ์๊ณ ์๋๋ผ๋ ์ง์ ๋ต์ ๊ตฌํด๋ณด๋ฉฐ ๋ฌธ์ ํ์ด์ ์ ๊ทผํด์ผ๊ฒ ๋ค!!!!
์ฝ์งํ ์๊ฐ์ ์๊น๊ณ ํ๋ฌดํ์ง๋ง.. ๋ค์๋ถํฐ ์ค์๋ฅผ ๋ฐ๋ณตํ์ง ์์ผ๋ฉด ๋๋ค..!
โป Comparable
public static class Node implements Comparable<Node>{
int destination, cost;
public Node(int destination, int cost) {
this.destination = destination;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return cost - o.cost;
}
}
์ฐ์ ์์ ํ์์ cost๋ฅผ ๋น๊ตํ๋ ์์ ์ด ํ์ํ๋ฏ๋ก Comparable ์ฌ์ฉ!
[๊ฐ์ฒด ๋น๊ต]
Comparable | Comparator |
์๊ธฐ์์ ๊ณผ ๋งค๊ฐ๋ณ์์ ๊ฐ์ฒด ๋น๊ต | ๋ ๋งค๊ฐ๋ณ์์ ๊ฐ์ฒด ๋น๊ต |
compareTo(T o) ํจ์ ๋ฌด์กฐ๊ฑด ๊ตฌํ | compare(T o1, T o2) ํจ์ ๋ฌด์กฐ๊ฑด ๊ตฌํ |
์ฐธ๊ณ : https://st-lab.tistory.com/243
๐ ํ์ด ๐
package Baekjoon;
import java.io.*;
import java.util.*;
public class Graph11779 {
public static class Node implements Comparable<Node>{
int destination, cost;
public Node(int destination, int cost) {
this.destination = destination;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return cost - o.cost;
}
}
public static int[] preNode; //์ต๋จ ๊ฒฝ๋ก๋ก ์ฌ ๊ฒฝ์ฐ ์ด์ ๋
ธ๋ ์ ์ฅํ ๋ฐฐ์ด
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
ArrayList<Node>[] graph = new ArrayList[n+1];
for(int i = 0; i <= n; i++) {
graph[i] = new ArrayList<>();
}
StringTokenizer st;
for(int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
graph[a].add(new Node(b, w));
}
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
preNode = new int[n+1];
bw.write(dijkstra(graph, start, end) + "\n");
bw.write(trace(start, end));
bw.flush();
bw.close();
}
/**
* ๋ค์ต์คํธ๋ผ
* @param graph ๋ฐฉํฅ ๊ฐ์ค์น ๊ทธ๋ํ
* @param start ์์ ์ ์
* @param end ๋์ฐฉ ์ ์
* @return ์์์์ ๋์ฐฉ๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ
*/
public static int dijkstra(ArrayList<Node>[] graph, int start, int end){
int[] distance = new int[graph.length];
Arrays.fill(distance, Integer.MAX_VALUE);
PriorityQueue<Node> heap = new PriorityQueue<>();
distance[start] = 0;
heap.offer(new Node(start, 0));
while (!heap.isEmpty()) {
Node current = heap.poll();
if(distance[current.destination] < current.cost) {
continue;
}
for(Node next : graph[current.destination]) {
if(distance[next.destination] > distance[current.destination] + next.cost) {
distance[next.destination] = distance[current.destination] + next.cost;
heap.offer(new Node(next.destination, distance[next.destination]));
preNode[next.destination] = current.destination; //๊ฒฝ๋ก๋ฅผ ์ฐพ๊ธฐ ์ํ ์ด์ ๋
ธ๋ ์ ์ฅ
}
}
}
return distance[end];
}
/**
* ๊ฒฝ๋ก ํ์
* @param start ์์ ์ ์
* @param end ๋์ฐฉ ์ ์
* @return ๋
ธ๋ ๊ฐ์ ๋ฐ ๊ฒฝ๋ก ๋ฌธ์์ด
*/
public static String trace(int start, int end) {
Stack<Integer> stack = new Stack<>();
stack.push(end);
int current = end;
while(preNode[current] != start) {
current = preNode[current];
stack.push(current);
}
StringBuilder answer = new StringBuilder();
answer.append(stack.size()+1 + "\n");
answer.append(start + " ");
while(!stack.isEmpty()) {
answer.append(stack.pop() + " ");
}
return answer.toString();
}
}
'ProgramSolve > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 17609๋ฒ - ํ๋ฌธ (Java) (0) | 2023.12.13 |
---|---|
[BOJ] 1806๋ฒ - ๋ถ๋ถํฉ (Java) (1) | 2023.12.12 |
[BOJ] 13270๋ฒ - ํผ๋ณด๋์น ์นํจ (Java) (2) | 2023.12.06 |
[BOJ] 12851๋ฒ - ์จ๋ฐ๊ผญ์ง2 (Java) (0) | 2023.12.04 |
[BOJ] 2437๋ฒ - ์ ์ธ (Python) (0) | 2023.10.27 |