
시작점에서 도착점으로 가는 최소 비용을 구하는 문제
-> 다익스트라 알고리즘을 활용해 풀 수 있음
가중치가 있고 방향이 있는 그래프
-> 우선순위 큐 사용, 방문 처리 배열로 최소 비용 갱신
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class 최소비용_구하기 {
static class Node implements Comparable<Node> {
int city, cost;
//생성자
public Node(int city, int cost) {
this.city = city;
this.cost = cost;
}
//compareTo 메소드 오버라이딩
public int compareTo(Node other) {
//오름차순 정렬(비용 작은 것부터)
return this.cost - other.cost;
}
}
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
//도시 개수
int N = Integer.parseInt(bf.readLine());
//버스 개수
int M = Integer.parseInt(bf.readLine());
//그래프 저장 인접리스트 생성
List<List<Node>> graph = new ArrayList<>();
//도시 번호가 1부터 시작해서 0~n까지 n+1개의 리스트
for (int i=0; i<=N; i++) {
graph.add(new ArrayList<>());
}
//출발 도시, 도착 도시, 비용 입력 받아 그래프에 넣기
for (int i=0; i<M; i++) {
st = new StringTokenizer(bf.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
//출발 도시 추가
graph.get(start).add(new Node(end, cost));
}
st = new StringTokenizer(bf.readLine());
//마지막 한 줄 입력(출발 도시, 도착 도시)
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
//1번부터 N번까지의 최소 비용 저장 배열
int[] distance = new int[N+1];
//모두 무한대로 일단 초기화
Arrays.fill(distance, Integer.MAX_VALUE);
//큐에 시작 노드 넣어주기
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(from, 0));
//시작 노드에서 자기 자신까지의 거리
distance[from] = 0;
//큐가 빌 때까지
while (!pq.isEmpty()) {
//가장 비용이 작은 도시 꺼내기
Node current = pq.poll();
//만약 이미 더 짧은 거리로 방문한적이 있다면
if (distance[current.city] < current.cost) {
//무시
continue;
}
//현재 도시에서 갈 수 있는 모든 도시 순회
for (Node next : graph.get(current.city)) {
//새로운 경로의 총 비용
int newCost = distance[current.city] + next.cost;
//만약 새로운 경로가 기존 경로보다 비용이 적다면
if (newCost < distance[next.city]) {
//갱신
distance[next.city] = newCost;
//큐에 집어넣어 그 도시에서의 경로도 조사
pq.add(new Node(next.city, newCost));
}
}
}
System.out.println(distance[to]);
}
}