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

Partially Committed

[๋ฐฑ์ค€ 1753] ์ตœ๋‹จ๊ฒฝ๋กœ (JAVA) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 1753] ์ตœ๋‹จ๊ฒฝ๋กœ (JAVA)

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

๋ฌธ์ œ

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

๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฃผ์–ด์ง„ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1 ≤ K ≤ V)๊ฐ€

www.acmicpc.net

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ 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๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

5 6
1
5 1 1
1 2 2
1 3 3
2 3 4
2 4 5
3 4 6

์˜ˆ์ œ ์ถœ๋ ฅ 1

0
2
3
7
INF

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

ArrayList<Node> graph [] ๋ฐฐ์—ด์— ์ฃผ์–ด์ง„ ๋…ธ๋“œ์™€ ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜์ž.

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ์ž‘ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ชจ๋“  ๋…ธ๋“œ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ดˆ๊ธฐ์—๋Š” ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์„ INF(0x7ffffff) ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.(์‹œ์ž‘๋…ธ๋“œ๋Š” 0์œผ๋กœ)

 

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์—์„œ ๊ฐ’์ด ๊ฐ€์žฅ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜์—ฌ, (์ด ๋ถ€๋ถ„์€ ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค.)

 

ํ•ด๋‹น ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ์—์ง€๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค์˜ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

 

์—…๋ฐ์ดํŠธ ์ •์ฑ…์€ Min(์„ ํƒํ•œ ๋…ธ๋“œ์˜ dist ๊ฐ’ ,  edge + ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ dist ๊ฐ’) ์ด๋‹ค.

 

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

public class Main {
    static int INF = Integer.MAX_VALUE;
    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 v = Integer.parseInt(stk.nextToken()); // ๋…ธ๋“œ ๊ฐœ์ˆ˜
        int e = Integer.parseInt(stk.nextToken()); // ์—์ง€ ๊ฐœ์ˆ˜

        stk = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(stk.nextToken()); // ์‹œ์ž‘์ 

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

        for (int i = 0; i < e; 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));
        }

        int[] dist = new int[v + 1];
        boolean[] visited = new boolean[v + 1];
        for (int i = 1; i <= v; i++) {
            dist[i] = INF;
        }

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.offer(new Node(start, 0));
        dist[start] = 0;

        while (!q.isEmpty()) {
            Node cur = q.poll();
            if (visited[cur.toNode]) continue; // ์ด๋ฏธ ๋ฐฉ๋ฌธ?
            visited[cur.toNode] = true;
            for (int i = 0; i < graph[cur.toNode].size(); i++) {
                Node t = graph[cur.toNode].get(i);
                int next = t.toNode;
                int val = t.e;
                if (dist[next] > dist[cur.toNode] + val) {
                    dist[next] = dist[cur.toNode] + val;
                    q.add(new Node(next, dist[next]));
                }
            }
        }

        for (int i = 1; i <= v; i++) {
            if (!visited[i])
                bw.write("INF\n");
            else bw.write(dist[i] + "\n");
        }
        br.close();
        bw.flush();
        bw.close();
    }

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

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

        public int compareTo(Node n) {
            if (this.e > n.e)
                return 1;
            else
                return -1;
        }
    }
}

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