Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 1854] K๋ฒˆ์งธ ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 1854] K๋ฒˆ์งธ ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ (JAVA)

WonderJay 2023. 1. 18. 11:38
728x90
๋ฐ˜์‘ํ˜•
SMALL
 

1854๋ฒˆ: K๋ฒˆ์งธ ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ

์ฒซ์งธ ์ค„์— n, m, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 1000, 0 ≤ m ≤ 2000000, 1 ≤ k ≤ 100) n๊ณผ m์€ ๊ฐ๊ฐ ๊น€ ์กฐ๊ต๊ฐ€ ์—ฌํ–‰์„ ๊ณ ๋ คํ•˜๊ณ  ์žˆ๋Š” ๋„์‹œ๋“ค์˜ ๊ฐœ์ˆ˜์™€, ๋„์‹œ ๊ฐ„์— ์กด์žฌํ•˜๋Š” ๋„๋กœ์˜ ์ˆ˜์ด๋‹ค. ์ด์–ด์ง€๋Š” m๊ฐœ์˜ ์ค„์—

www.acmicpc.net

๋ด„์บ ํ”„๋ฅผ ๋งˆ์นœ ๊น€์ง„์˜ ์กฐ๊ต๋Š” ์—ฌ๋Ÿฌ ๋„์‹œ๋ฅผ ๋Œ๋ฉฐ ์—ฌํ–‰์„ ๋‹ค๋‹ ๊ณ„ํš์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊น€ ์กฐ๊ต๋Š”, '๋Š๋ฆผ์˜ ๋ฏธํ•™'์„ ์ค‘์š”์‹œํ•˜๋Š” ์‚ฌ๋žŒ์ด๋ผ ํ•ญ์ƒ ์ตœ๋‹จ๊ฒฝ๋กœ๋กœ๋งŒ ์ด๋™ํ•˜๋Š” ๊ฒƒ์€ ๋ณ„๋กœ ์ข‹์•„ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๋„ˆ๋ฌด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ๋กœ๋„ ๊ทธ๋ฆฌ ๋งค๋ ฅ์ ์ธ ๊ฒƒ๋งŒ์€ ์•„๋‹ˆ์–ด์„œ, ์ ๋‹นํ•œ ํƒ€ํ˜‘์•ˆ์ธ '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์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ตœ๋‹จ๊ฒฝ๋กœ์— ๊ฐ™์€ ์ •์ ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ํฌํ•จ๋˜์–ด๋„ ๋œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 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

์˜ˆ์ œ ์ถœ๋ ฅ 1

-1
10
7
5
14

 


์–ด๋ ต๋„ค...๐Ÿ˜’

 

์ผ๋ฐ˜์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ์™€๋Š” ๋‹ฌ๋ฆฌ ์‹œ์ž‘๋…ธ๋“œ์—์„œ ๋„์ฐฉ๋…ธ๋“œ๊นŒ์ง€ k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด๋‚ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ํฌ์ธํŠธ๋ฅผ ์ƒ๊ฐํ•ด์•ผํ•œ๋‹ค.

 

1. k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ, ๊ธฐ์กด์— ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉํ•˜๋˜ 1์ฐจ์› int ํ˜• ๋ฐฐ์—ด์„ dist ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๊ฒ ๋‹ค. ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ i ๋…ธ๋“œ์— ๋Œ€ํ•œ k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค. ๊ทผ๋ฐ ์ •๋ ฌ ์ˆœ์„œ๋„ ์œ ์ง€ํ–ˆ์Œ์ข‹๊ฒŸ๋‹ค.. 

โ–ถ ์šฐ์„ ์ˆœ์œ„ํ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์ž.

 

2. k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ™์€ ๋…ธ๋“œ๋ฅผ ์žฌ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ visited ๋ฐฐ์—ด๋กœ ๋ฐฉ์ง€ํ–ˆ์—ˆ๋Š”๋ฐ.

โ–ถ visited ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

3. k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋„๋ก ํ• ๊นŒ ์•„๋‹ˆ๋ฉด ๋ชจ๋“  ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค ์ €์žฅํ•˜๋„๋ก ํ•ด์„œ ์ถœ๋ ฅํ•  ๋•Œ k ๊ฐœ๋งŒ ๋ฝ‘์•„๋‚ผ๊นŒ? ๋‹น์—ฐํžˆ, k ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋งŒ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํšจ์œจ์ ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜์ง€?

โ–ถ ํ˜„์žฌ ๋…ธ๋“œ์— ํ•ด๋‹นํ•˜๋Š” ์šฐ์„ ์ˆœ์œ„ ํ ๋ฐฐ์—ด์— k ๊ฐœ ๋ณด๋‹ค ์ž‘์€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋งŒ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๋ฉด, ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฐœ๊ฒฌํ•œ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•œ๋‹ค. (์šฐ์„ ์ˆœ์œ„ ํ์˜ ์ •๋ ฌ ์ •์ฑ…์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.) ๊ทผ๋ฐ ๋งŒ์•ฝ์— k ๊ฐœ ์ด์ƒ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋Š”๋ฐ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด?

โ–ถ ๊ทธ๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ํ˜„์žฌ ์šฐ์„ ์ˆœ์œ„ํ ๋ฐฐ์—ด์— ์ €์žฅ๋œ top ๊ณผ ๋น„๊ตํ•œ๋‹ค. i ๋ฒˆ์งธ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์šฐ์„ ์ˆœ์œ„ํ ๋ฐฐ์—ด์˜ top ์€, "i ๋ฒˆ์งธ ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๋Š” ํ˜„์žฌ๊นŒ์ง€ ์ €์žฅ๋œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’" ์ด๋‹ค. ์ด ๊ฐ’๋ณด๋‹ค ์ƒˆ๋กœ ๊ณ„์‚ฐ๋œ ๊ฒฝ๋กœ๊ฐ€ ์ž‘๋‹ค๋ฉด, top ์„ poll() ํ•ด์ฃผ๊ณ , ์ƒˆ๋กœ ๊ณ„์‚ฐ๋œ ๊ฒฝ๋กœ๋ฅผ add ํ•ด์ค€๋‹ค. 

โ–ถ ์ด๋Ÿฌํ•œ ๊ณผ์ •์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋Œ๋ ค์ฃผ๋ฉด, ์šฐ์„ ์ˆœ์œ„ํ ๋ฐฐ์—ด์˜ size ๊ฐ€ k ์ด๋ฉด top ์ด k ๋ฒˆ์งธ ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ด๊ณ , size ๊ฐ€ k ๊ฐ€ ์•„๋‹ˆ๋ฉด k ๋ฒˆ์งธ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค.

โ–ถ ์œ ์˜ํ•  ์ ์€ ๋ฌธ์ œ์—์„œ 1 ๋ฒˆ๋…ธ๋“œ์—์„œ 1 ๋ฒˆ๋…ธ๋“œ๋กœ ํ–ฅํ•˜๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” 0 ์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹œ์ž‘ํ•  ๋•Œ, ์ดˆ๊ธฐ ๋…ธ๋“œ๋ฅผ ์ด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    static ArrayList<Node>[] graph;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(stk.nextToken()); // ๋…ธ๋“œ ๊ฐœ์ˆ˜
        int m = Integer.parseInt(stk.nextToken()); // ์—์ง€ ๊ฐœ์ˆ˜
        int k = Integer.parseInt(stk.nextToken()); // k
        // ๋จผ์ € ๊ทธ๋ž˜ํ”„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ž.
        graph = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++)
            graph[i] = new ArrayList<Node>();
        for (int i = 0; i < m; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            int cost = Integer.parseInt(stk.nextToken());
            graph[a].add(new Node(b, cost));
        }
        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; i++) {
            distQueue[i] = new PriorityQueue<>(k, cp);
        }
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(new Node(1, 0)); // ์‹œ์ž‘์ 
        distQueue[1].add(0);
        while (!pq.isEmpty()) {
            Node now_node = pq.poll();
            for (Node cur : graph[now_node.toNode]) {
                // ์—ฐ๊ฒฐ๊ด€๊ณ„ ํƒ์ƒ‰
                if (distQueue[cur.toNode].size() < k) {
                    // ์ €์žฅ๋œ ๊ฒฝ๋กœ๊ฐ€ k ๊ฐœ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ฒฝ๋กœ ์ €์žฅ
                    distQueue[cur.toNode].add(now_node.w + cur.w);
                    pq.add(new Node(cur.toNode, now_node.w + cur.w));
                } else if (distQueue[cur.toNode].peek() > now_node.w + cur.w) {
                    // ์ €์žฅ๋œ ๊ฒฝ๋กœ๊ฐ€ k ๊ฐœ์ด๊ณ , ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ๊ฐ€ ํ˜„์žฌ ๊ฐ€์žฅ ํฐ ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ถ”๊ฐ€
                    distQueue[cur.toNode].poll();
                    distQueue[cur.toNode].add(now_node.w + cur.w);
                    pq.add(new Node(cur.toNode, now_node.w + cur.w));
                }
            }
        }
        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();
    }

    static class Node implements Comparable<Node> {
        int toNode;
        int w;

        public Node(int toNode, int w) {
            this.toNode = toNode;
            this.w = w;
        }

        @Override
        public int compareTo(Node v) {
            return this.w < v.w ? -1 : 1;
        }

    }
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments