본문 바로가기
JAVA/백준

[Java] 백준 1854 K번째 최단경로 찾기 - 다익스트라(3)

by 푸_푸 2022. 12. 10.
728x90

백준 1854 K번째 최단경로 찾기
문제

봄캠프를 마친 김진영 조교는 여러 도시를 돌며 여행을 다닐 계획이다. 그런데 김 조교는, '느림의 미학'을 중요시하는 사람이라 항상 최단경로로만 이동하는 것은 별로 좋아하지 않는다. 하지만 너무 시간이 오래 걸리는 경로도 그리 매력적인 것만은 아니어서, 적당한 타협안인 'k번째 최단경로'를 구하길 원한다. 그를 돕기 위한 프로그램을 작성해 보자.

입력

첫째 줄에 n, m, k가 주어진다. (1 ≤ n ≤ 1000, 0 ≤ m ≤ 2000000, 1 ≤ k ≤ 100) n과 m은 각각 김 조교가 여행을 고려하고 있는 도시들의 개수와, 도시 간에 존재하는 도로의 수이다.

이어지는 m개의 줄에는 각각 도로의 정보를 제공하는 세 개의 정수 a, b, c가 포함되어 있다. 이것은 a번 도시에서 b번 도시로 갈 때는 c의 시간이 걸린다는 의미이다. (1 ≤ a, b ≤ n. 1 ≤ c ≤ 1000)

도시의 번호는 1번부터 n번까지 연속하여 붙어 있으며, 1번 도시는 시작도시이다.

출력

n개의 줄을 출력한다. i번째 줄에 1번 도시에서 i번 도시로 가는 k번째 최단경로의 소요시간을 출력한다.

경로의 소요시간은 경로 위에 있는 도로들을 따라 이동하는데 필요한 시간들의 합이다. i번 도시에서 i번 도시로 가는 최단경로는 0이지만, 일반적인 k번째 최단경로는 0이 아닐 수 있음에 유의한다. 또, k번째 최단경로가 존재하지 않으면 -1을 출력한다. 최단경로에 같은 정점이 여러 번 포함되어도 된다.


제출

import java.io.*;
import java.util.*;
public class Main {
	static final int INF = 100000000;
	public static void main(String[] args) throws IOException {
		int N, M, K;
		int[][] W = new int[1001][1001];
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		K = Integer.parseInt(st.nextToken());
		PriorityQueue<Integer>[] distQueue = new PriorityQueue[N + 1];
		Comparator<Integer> cp = new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o1 < o2 ? 1 : -1;
			}
		};
		for (int i = 0; i < N + 1; i++) {
			distQueue[i] = new PriorityQueue<Integer>(K, cp);
		}
		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 c = Integer.parseInt(st.nextToken());
			W[a][b] = c;
		}
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(1, 0));
		distQueue[1].add(0);
		while (!pq.isEmpty()) {
			Node u = pq.poll();
			for (int adjNode = 1; adjNode <= N; adjNode++) {
				if (W[u.node][adjNode] != 0) {
					if (distQueue[adjNode].size() < K) {
						distQueue[adjNode].add(u.cost + W[u.node][adjNode]);
						pq.add(new Node(adjNode, u.cost + W[u.node][adjNode]));
					}
					else if (distQueue[adjNode].peek() > u.cost + W[u.node][adjNode]) {
						distQueue[adjNode].poll();
						distQueue[adjNode].add(u.cost + W[u.node][adjNode]);
						pq.add(new Node(adjNode, u.cost + W[u.node][adjNode]));
					}
				}
			}
		}
		for (int i = 1; i <= N; i++) {
			if (distQueue[i].size() == K) {
				bw.write(distQueue[i].peek() + "\n");
			} else {
				bw.write(-1 + "\n");
			}
		}
		bw.flush();
		bw.close();
		br.close();
	}
}
class Node implements Comparable<Node>{
	int node;
	int cost;
	Node(int node, int cost){
		this.node = node;
		this.cost = cost;
	}
	@Override
	public int compareTo(Node o) {
		return this.cost < o.cost ? -1 : 1;
	}
}

예제

5 10 2
1 2 2
1 3 7
1 4 5
1 5 6
2 4 2
2 3 4
3 4 6
3 5 8
5 2 4
5 4 1

결과

백준 1854 K번째 최단경로 찾기

 

 

1854번: K번째 최단경로 찾기

첫째 줄에 n, m, k가 주어진다. (1 ≤ n ≤ 1000, 0 ≤ m ≤ 2000000, 1 ≤ k ≤ 100) n과 m은 각각 김 조교가 여행을 고려하고 있는 도시들의 개수와, 도시 간에 존재하는 도로의 수이다. 이어지는 m개의 줄에

www.acmicpc.net

 

728x90

댓글