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

Partially Committed

[๋ฐฑ์ค€ 1162 ๋„๋กœํฌ์žฅ] ์ž๋ฐ” (๋‹ค์ต+DP) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 1162 ๋„๋กœํฌ์žฅ] ์ž๋ฐ” (๋‹ค์ต+DP)

WonderJay 2023. 5. 28. 14:15
728x90
๋ฐ˜์‘ํ˜•
SMALL

1162๋ฒˆ: ๋„๋กœํฌ์žฅ (acmicpc.net)

 

1162๋ฒˆ: ๋„๋กœํฌ์žฅ

์ฒซ ์ค„์—๋Š” ๋„์‹œ์˜ ์ˆ˜ N(1 ≤ N ≤ 10,000)๊ณผ ๋„๋กœ์˜ ์ˆ˜ M(1 ≤ M ≤ 50,000)๊ณผ ํฌ์žฅํ•  ๋„๋กœ์˜ ์ˆ˜ K(1 ≤ K ≤ 20)๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. M๊ฐœ์˜ ์ค„์— ๋Œ€ํ•ด ๋„๋กœ๊ฐ€ ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ๋„์‹œ์™€ ๋„๋กœ๋ฅผ ํ†ต๊ณผํ•˜

www.acmicpc.net

 

 ๋‹ค์ต์ŠคํŠธ๋ผ + dp

์ด์ „์— **๋ฏธํ™•์ธ ๋„์ฐฉ์ง€ ๋ฌธ์ œ**๋ฅผ ํ†ตํ•ด ๋‹ค์ต์ŠคํŠธ๋ผ์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ๋ฅผ ๊ตฌํ˜„ํ–ˆ์—ˆ๋‹ค.

๋„๋กœํฌ์žฅ์€ ๋‹ค์ต์ŠคํŠธ๋ผ์— dp ๋ฅผ ๊ณ๋“ค์ธ ๋ฌธ์ œ๋‹ค ๐Ÿ˜

๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํžˆ ์š”์•ฝํ•˜๋ฉด,

์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด

1๋ฒˆ ๋…ธ๋“œ์—์„œ N ๋ฒˆ ๋…ธ๋“œ๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์›€์ง์—ฌ์•ผ ํ•œ๋‹ค.

์ด๋•Œ, ์ข€ ๋” ๋น ๋ฅด๊ฒŒ ์›€์ง์ด๊ธฐ ์œ„ํ•ด **๋„๋กœ ํฌ์žฅ** ์„ k ๋ฒˆ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋„๋กœ ํฌ์žฅ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด, ํ•ด๋‹น ๋„๋กœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฐ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€,

์ฃผ์–ด์ง„ k ๋ฒˆ ์ดํ•˜์˜ ๋„๋กœ ํฌ์žฅ์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ๋ชฉ์ ์ง€์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

(๋„๋กœ ํฌ์žฅ์„ ํ•œ ๋ฒˆ๋„ ์•ˆํ•˜๋Š” ๊ฒŒ ์ตœ๋‹จ ๊ฒฝ๋กœ์ผ ์ˆ˜๋„ ์žˆ๋‹ค.)

---

๋‹ค์ต์ŠคํŠธ๋ผ์—์„œ๋Š” dist ๋ฐฐ์—ด์„ 1์ฐจ์›์œผ๋กœ ์„ ์–ธํ•˜์—ฌ, ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฒˆ์—๋Š” ๋„๋กœ๋ฅผ ํฌ์žฅํ–ˆ๋Š”์ง€, ํ–ˆ๋‹ค๋ฉด ๋ช‡ ๋ฒˆ ํ–ˆ๋Š”์ง€ ๊นŒ์ง€ ๊ณ ๋ คํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผํ•œ๋‹ค.

dist ๋ฐฐ์—ด์„ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ํ†ตํ•ด ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ณ„์† update ํ•œ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๊ฑธ์–ด์˜จ ์ตœ๋‹จ๊ฒฝ๋กœ(sub problem)์™€ ์•ž์œผ๋กœ ๊ฐˆ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๋น„๊ตํ•˜์—ฌ update ํ•ด์„œ ์ตœ์ข…์ ์ธ ๊ฒฝ๋กœ๋ฅผ ์–ป๋Š”๋‹ค. -> ์—ฌ๊ธฐ์„œ dp ์˜ ๊ฐœ๋…์ด ์ฒจ๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ ํ•˜๋‚˜ ๋” ๋‚˜์•„๊ฐ€์„œ

์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ, ์•ž์œผ๋กœ ๊ฐˆ ๋„๋กœ๋ฅผ ํฌ์žฅํ•˜๋Š” ์ผ€์ด์Šค์™€ ํ•˜์ง€ ์•Š๋Š” ์ผ€์ด์Šค๋ฅผ ๋ชจ๋‘ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

์ด๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ..?

์‚ฌ์‹ค, ์ ‘ํ•ด๋ณด์ง€ ์•Š์œผ๋ฉด ๋– ์˜ฌ๋ฆฌ๊ธฐ ํž˜๋“  ๊ฒƒ ๊ฐ™๋‹ค..

๋‹ต์€ i ๋ฒˆ ๋…ธ๋“œ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ๊นŒ์ง€์˜ ๋„๋กœ ํฌ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋ฏ€๋กœ

dist ๋ฐฐ์—ด์— ๋„๋กœ ํฌ์žฅ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฐจ์›์„ ์ถ”๊ฐ€ํ•˜๊ณ 

Node ์—๋„ ๋„๋กœ ํฌ์žฅ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

while(pq๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€){
    now ๋Š” ํ˜„์žฌ  ๋‚ด๊ฐ€ ์„œ ์žˆ๋Š” ๋…ธ๋“œ
    for( now ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ iter ์„ ํƒ์ƒ‰) {
      if( now ์—์„œ iter ์œผ๋กœ ๊ฐ€๋Š”๊ฒŒ ์ตœ๋‹จ ๊ฒฝ๋กœ์ด๋ฉด){
          iter ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ, now ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ + iter ์˜ ๊ฐ€์ค‘์น˜๋กœ ๊ฐฑ์‹ 
          pq ์— ์ถ”๊ฐ€
      }
    }



์—ฌ๊ธฐ์„œ ํฌ์žฅ ํšŸ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๊ณ ๋ คํ•˜๋ฉด,



while(pq๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€){
    now ๋Š” ํ˜„์žฌ  ๋‚ด๊ฐ€ ์„œ ์žˆ๋Š” ๋…ธ๋“œ
    for( now ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ iter ์„ ํƒ์ƒ‰) {
        if( now ์˜ ํฌ์žฅ ํšŸ์ˆ˜์ธ now.cnt ๊ฐ€ k ๋ณด๋‹ค ์ž‘์•„์„œ, ๋„๋กœ๋ฅผ ํฌ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด && ๋„๋กœ๋ฅผ ํฌ์žฅํ–ˆ์„ ๋•Œ ๋” ๋นจ๋ฆฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด){
            now.cnt+1 ๋งŒํผ ๋„๋กœ ํฌ์žฅ์„ ์ˆ˜ํ–‰ํ•œ, iter ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ update
            pq ์— ์ถ”๊ฐ€
        }
        if( now.cnt ๊ฐ€ k ์ด์ƒ์ด๋ผ์„œ ๋„๋กœ๋ฅผ ๋”์ด์ƒ ํฌ์žฅํ•  ์ˆ˜ ์—†๋‹ค๋ฉด && iter ์œผ๋กœ ๊ฐ€๋Š” ๊ฒŒ ๋” ์งง์€ ๊ฑฐ๋ฆฌ๋ผ๋ฉด){
            now.cnt ๋งŒํผ ๋„๋กœ ํฌ์žฅ์„ ์ˆ˜ํ–‰ํ•œ, iter ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ update
            pq ์— ์ถ”๊ฐ€
        }
    }



์œ„์™€ ๊ฐ™์ด ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.



๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋Œ€์ถฉ 40~50% ์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋œฐ ๊ฒƒ์ด๋‹ค. ๐Ÿซ 

pq ์— ๋„ˆ๋ฌด ๋งŽ์€ ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋˜๊ฐ€ ํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์ผํ…๋ฐ...

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ค‘๋ณต๋œ ์—ฐ์‚ฐ์„ ์ค„์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ฆ‰, ํƒ์ƒ‰ํ•˜์ง€ ์•Š์•„๋„ ๋  ๋…ธ๋“œ๋ฅผ cut ํ•˜์—ฌ ์‹œ๊ฐ„์„ ์ตœ์ ํ™”ํ•˜๋Š” ๊ณผ์ •์ด ํ•˜๋‚˜ ๋” ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ฏธ ์ฒ˜๋ฆฌํ•œ ๋…ธ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋„๋ก ์กฐ๊ฑด์„ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค!

 

---

 

import java.io.*;
import java.util.*;

public class Main {
    static int n; // ๋…ธ๋“œ
    static int m; // ์—ฃ์ง€
    static int k; // ํฌ์žฅํ•  ๋„๋กœ ๊ฐœ์ˆ˜
    static long[][] dist;
    static ArrayList<Node>[] graph;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk = new StringTokenizer(br.readLine());

        n = Integer.parseInt(stk.nextToken());
        m = Integer.parseInt(stk.nextToken());
        k = Integer.parseInt(stk.nextToken());

        graph = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        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 w = Integer.parseInt(stk.nextToken());
            graph[a].add(new Node(b, w, 0));
            graph[b].add(new Node(a, w, 0));
        }

        dist = new long[n + 1][k + 1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dist[i], Long.MAX_VALUE);
        }

        dist[1][0] = 0;

        // do dijkstra
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(new Node(1, 0, 0));
        while (!pq.isEmpty()) {
            Node now = pq.poll();
            if (now.w > dist[now.to][now.cnt])
                continue;
            for (Node iter : graph[now.to]) {
                if (now.cnt < k && dist[iter.to][now.cnt + 1] > dist[now.to][now.cnt]) {
                    dist[iter.to][now.cnt + 1] = dist[now.to][now.cnt];
                    pq.add(new Node(iter.to, dist[iter.to][now.cnt + 1], now.cnt + 1));
                }
                if (dist[iter.to][now.cnt] > dist[now.to][now.cnt] + iter.w) {
                    dist[iter.to][now.cnt] = dist[now.to][now.cnt] + iter.w;
                    pq.add(new Node(iter.to, dist[iter.to][now.cnt], now.cnt));
                }
            }
        }

        long ans = Long.MAX_VALUE;
        for (int i = 0; i <= k; i++) {
            ans = Math.min(dist[n][i], ans);
        }
        bw.write(ans + "\n");
        bw.flush();
        bw.close();
    }

    static class Node implements Comparable<Node> {
        int to;
        long w;
        int cnt;

        public Node(int to, long w, int cnt) {
            this.to = to;
            this.w = w;
            this.cnt = cnt;
        }

        @Override
        public int compareTo(Node o) {
            return (int) (this.w - o.w);
        }
    }
}

 

 

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