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

Partially Committed

[๋ฐฑ์ค€ 1916] ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ (JAVA) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 1916] ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ (JAVA)

WonderJay 2023. 1. 17. 16:52
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

https://www.acmicpc.net/problem/1916

 

1916๋ฒˆ: ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ M+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ

www.acmicpc.net

N๊ฐœ์˜ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค๋ฅธ ๋„์‹œ์— ๋„์ฐฉํ•˜๋Š” M๊ฐœ์˜ ๋ฒ„์Šค๊ฐ€ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” A๋ฒˆ์งธ ๋„์‹œ์—์„œ B๋ฒˆ์งธ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ๋ฒ„์Šค ๋น„์šฉ์„ ์ตœ์†Œํ™” ์‹œํ‚ค๋ ค๊ณ  ํ•œ๋‹ค. A๋ฒˆ์งธ ๋„์‹œ์—์„œ B๋ฒˆ์งธ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ๋น„์šฉ์„ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ M+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ค์Œ์—๋Š” ๋„์ฐฉ์ง€์˜ ๋„์‹œ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๋˜ ๊ทธ ๋ฒ„์Šค ๋น„์šฉ์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฒ„์Šค ๋น„์šฉ์€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘์€ ์ •์ˆ˜์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  M+3์งธ ์ค„์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ตฌ๊ฐ„ ์ถœ๋ฐœ์ ์˜ ๋„์‹œ๋ฒˆํ˜ธ์™€ ๋„์ฐฉ์ ์˜ ๋„์‹œ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ถœ๋ฐœ์ ์—์„œ ๋„์ฐฉ์ ์„ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ถœ๋ฐœ ๋„์‹œ์—์„œ ๋„์ฐฉ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

5
8
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
1 5

์˜ˆ์ œ ์ถœ๋ ฅ 1 ๋ณต์‚ฌ

4

๊ฐ€์ค‘์น˜๊ฐ€ ์–‘์ˆ˜์ด๋ฏ€๋กœ , ๋‹จ์ˆœํžˆ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

์ฃผ์–ด์ง„ ์ตœ๋Œ€ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 1000 ์ด๊ณ  ์—์ง€์˜ ๊ฐœ์ˆ˜ ๋˜ํ•œ ์ตœ๋Œ€ 100,000 ์ด๋ฏ€๋กœ

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(ElogV) = O(10,000 * 3) ์œผ๋กœ ์ƒ๋‹นํžˆ ๋„‰๋„‰ํ•˜๋‹ค!

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ๊ฐ๊ฐ์˜ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ์—, ๋„์ฐฉ ๋„์‹œ์— ๋Œ€ํ•œ ๋น„์šฉ์„ ์ถœ๋ ฅํ•˜์ž.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    static ArrayList<Node>[] graph;
    static final int INF = Integer.MAX_VALUE;
    static boolean[] visited;
    static int[] dist;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stk;

        int n = Integer.parseInt(br.readLine());
        int m = Integer.parseInt(br.readLine());

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

        dist = new int[n + 1];
        Arrays.fill(dist, INF);

        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 e = Integer.parseInt(stk.nextToken());
            graph[a].add(new Node(b, e));
        }
        stk = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(stk.nextToken());
        int end = Integer.parseInt(stk.nextToken());

        visited = new boolean[n + 1];
        dijkstra(start, end);
        System.out.println(dist[end]);
    }

    public static void dijkstra(int start, int end) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.offer(new Node(start, 0));
        dist[start] = 0;
        while (!pq.isEmpty()) {
            Node now = pq.poll();
            if (!visited[now.toNode]) {
                visited[now.toNode] = true;
                for (Node cur : graph[now.toNode]) {
                    if (!visited[cur.toNode] && dist[cur.toNode] > dist[now.toNode] + cur.w) {
                        dist[cur.toNode] = dist[now.toNode] + cur.w;
                        pq.offer(new Node(cur.toNode, dist[cur.toNode]));
                    }
                }
            }
        }
    }

    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;
        }
    }
}

 

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