-
백준 1753번 최단경로 (JAVA)알고리즘(Algorithm)/다익스트라(Dijkstra) 2021. 4. 7. 00:59
백준 1753번 최단경로
https://www.acmicpc.net/problem/1753
1753번: 최단경로
첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다.
www.acmicpc.net
문제
방향그래프가 주어지면 주어진 시작점에서 다른 모든 정점으로의 최단 경로를 구하는 프로그램을 작성하시오. 단, 모든 간선의 가중치는 10 이하의 자연수이다.
입력
첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1 ≤ K ≤ V)가 주어진다. 셋째 줄부터 E개의 줄에 걸쳐 각 간선을 나타내는 세 개의 정수 (u, v, w)가 순서대로 주어진다. 이는 u에서 v로 가는 가중치 w인 간선이 존재한다는 뜻이다. u와 v는 서로 다르며 w는 10 이하의 자연수이다. 서로 다른 두 정점 사이에 여러 개의 간선이 존재할 수도 있음에 유의한다.
출력
첫째 줄부터 V개의 줄에 걸쳐, i번째 줄에 i번 정점으로의 최단 경로의 경로값을 출력한다. 시작점 자신은 0으로 출력하고, 경로가 존재하지 않는 경우에는 INF를 출력하면 된다.
소스 코드
- 다익스트라 기본 문제
- 단방향 다익스트라
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { private static int V, E, K; //V : 정점, E : 간선, K : 시작 정점의 번호 private static int INF = Integer.MAX_VALUE; private static ArrayList<Node>[] adjList; private static int[] cost; private static boolean[] visited; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; st = new StringTokenizer(br.readLine()); V = Integer.parseInt(st.nextToken()); E = Integer.parseInt(st.nextToken()); K = Integer.parseInt(br.readLine()); adjList = new ArrayList[V +1]; cost = new int[V + 1]; visited = new boolean[V+1]; //인접리스트 초기화 for (int i = 0; i < V+1; i++) { adjList[i] = new ArrayList<>(); } for (int i = 0; i < E; i++) { st = new StringTokenizer(br.readLine()); int u = Integer.parseInt(st.nextToken()); int v = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken()); adjList[u].add(new Node(v, w)); //무방향 그래프일 경우 추가 //adjList[v].add(new Node(u, w)); } dijkstra(K); for (int i = 1; i <= V; i++) { if(cost[i] == INF) bw.write("INF\n"); else bw.write(cost[i] + "\n"); } bw.flush(); bw.close(); br.close(); } private static void dijkstra(int start) { //탐색경로 저장 queue(기준은 최저 비용) PriorityQueue<Node> pq = new PriorityQueue<>(); //cost 배열 무한대로 초기화 for (int i = 1; i <= V; i++) { cost[i] = INF; } //visited 배열 초기화 Arrays.fill(visited, false); cost[start] = 0; //시작 지점까지의 비용은 0 pq.add(new Node(start, 0)); //비용 갱신 위한 기준 노드 while (!pq.isEmpty()) { Node now = pq.poll(); if(visited[now.dest]) continue; visited[now.dest] = true; //연결된 간선 탐색 for (Node next : adjList[now.dest]) { //가지고 도착한 비용 + 연결된 간선 비용이, 기존에 저장된 dest까지의 비용보다 작으면 cost 갱신 if(cost[next.dest] > next.cost + now.cost) { cost[next.dest] = next.cost + now.cost; pq.add(new Node(next.dest, cost[next.dest])); } } } } private static class Node implements Comparable<Node>{ int dest, cost; public Node(int dest, int cost) { this.dest = dest; this.cost = cost; } @Override public int compareTo(Node o) { return this.cost - o.cost; //cost 기준 오름차순 } } }
'알고리즘(Algorithm) > 다익스트라(Dijkstra)' 카테고리의 다른 글
백준 1238번 파티 (JAVA) (0) 2022.04.15 백준 1504번 특정한 최단 경로 (JAVA) (0) 2022.04.15 백준 1916번 최소비용 구하기(JAVA) (0) 2021.04.08