[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฐฑ์ค€ 1753๋ฒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฐ๋‹ฌ

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฐœ๋…


๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? 

ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
Single source shortest paths problem

 

๋กœ์ง

  1. ๋ชจ๋“  ์ •์ ๋“ค์€ ์ฒ˜์Œ์— ๋ฌดํ•œ๋Œ€ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. 
  2. ์ •์  A์—์„œ ์ •์  B๊ฐ€ ์ด์–ด์ง€๋ฉด ์ •์  A๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ + A์—์„œ B๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  3. ๋งŒ์•ฝ B๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ’(์ฆ‰, A์—์„œ B๋กœ ์ด์–ด์ง€๋Š” ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ์˜ํ•œ ๊ฐ’)๊ณผ 2๋ฒˆ์—์„œ ๊ณ„์‚ฐํ•œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ํ›„์ž๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด ๊ฐฑ์‹ ํ•œ๋‹ค.
  4. ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ ๋ถ€ํ„ฐ ์ด์–ด์„œ ๋ฐ˜๋ณตํ•œ๋‹ค. (๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ด์œ ๋Š” ์ž‘์ง€ ์•Š์€ ๊ฐ’๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๋ฉด ์“ธ๋ชจ์—†๋Š” ์—ฐ์‚ฐ์ด ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋ฉฐ, ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์—†๋‹ค.)  
์ž‘์€ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ์ด ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ํฐ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๋ฉด ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ? ์—†์Œ.

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„


๊ตฌํ˜„ ์‹œ ์ฃผ์˜์  

  • ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง„ ์ •์ ์„ ์–ป์–ด๋‚ด๋„๋ก ํ•œ๋‹ค.
  • JAVA์˜ Priority Queue๋Š” poll(), add()์™€ ๊ฐ™์ด ์ถ”๊ฐ€,์‚ญ์ œ ์—ฐ์‚ฐ์ด ์ผ์–ด๋‚  ๋•Œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ฐฑ์‹ ํ•  ๋•Œ, ๊ฐ’์ด ๋ฐ”๋€ ๋ฐ์ดํ„ฐ๋งŒ ๋‹ค์‹œ ์‚ฝ์ž…ํ•˜๋„๋ก ํ•œ๋‹ค. 
  • ๋งŒ์•ฝ ์ธ์ ‘ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„์„ ํ•˜๊ฒŒ ๋˜์—ˆ์„ ๋•Œ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(V^2)์ด๋ฉฐ, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„์„ ํ•˜๊ฒŒ ๋˜๋ฉด O(VE) ์ด๋‹ค. 

 

๊ตฌํ˜„ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class ShortestPath {
    
    static Node[] nodes; 
    static List<List<int[]>> edge_weights;
    static boolean[] isChecked;
    static public final int INF = 1000000000;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int vertexCnt,edgeCnt; 
        
        String[] line = br.readLine().split(" "); 
        vertexCnt = Integer.parseInt(line[0]);
        edgeCnt = Integer.parseInt(line[1]);
        edge_weights = new ArrayList<>();
        nodes = new Node[vertexCnt+1];
        isChecked = new boolean[vertexCnt+1];
        
        // Node๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” list๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
        // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
        edge_weights.add(new ArrayList<>());
        for(int cnt=1; cnt<=vertexCnt; cnt++) {
            nodes[cnt] = new Node(cnt);
            edge_weights.add(new ArrayList<>());
        }
        
        int startVertex = Integer.parseInt(br.readLine());
        nodes[startVertex].shortest_distance = 0;
        
        // ๊ฐ„์„  ๊ฐ€์ค‘์น˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
        for(int cnt=0; cnt<edgeCnt; cnt++) {
            line = br.readLine().split(" "); 
            int start = Integer.parseInt(line[0]);
            
            int[] end_weight = new int[2];
            end_weight[0= Integer.parseInt(line[1]);
            end_weight[1= Integer.parseInt(line[2]);
            
            edge_weights.get(start).add(end_weight);
        }
        
        // ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. 
        dijkstra(startVertex, vertexCnt);
        
        printNodes();
        
        line = null
        nodes = null;
        edge_weights = null;
    }
    
    private static void dijkstra(int startVertex, int vertexCnt) {
        PriorityQueue<Node> pqueue = new PriorityQueue<>();
        //๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ ์šฐ์„ ์ˆœ์œ„ ํž™์— ๋„ฃ๋Š”๋‹ค.
        for(int cnt=1; cnt<=vertexCnt; cnt++) {
            pqueue.add(nodes[cnt]);
        }
        
        //๊ฐ€์žฅ ๊ฒฝ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ์„ ํƒํ•œ๋‹ค.
        //๋งŒ์•ฝ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ shortest path๋ฅผ ์ตœ์‹ ํ™” ํ•œ๋‹ค.
        //๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ์„ ํƒ๋˜์—ˆ์œผ๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.
        while(!pqueue.isEmpty()) {
            Node start = pqueue.poll();
            isChecked[start.num] = true;
            
            for(int[] list : edge_weights.get(start.num)) {
                int before = nodes[list[0]].shortest_distance;
                nodes[list[0]].shortest_distance = 
                        Math.min(nodes[list[0]].shortest_distance, start.shortest_distance + list[1]);  
                
                if(nodes[list[0]].shortest_distance != before) {
                    pqueue.remove(nodes[list[0]]);
                    pqueue.add(nodes[list[0]]);
                }
            }
        }
        pqueue = null;
    }
    
    private static void printNodes() {
        for(int idx=1; idx<nodes.length; idx++) {
            if(nodes[idx].shortest_distance == INF) {
                System.out.println("INF");
            }
            else 
                System.out.println(nodes[idx].shortest_distance);
        }
        System.out.println();
    }
    
    static class Node implements Comparable<Node> {
        int num;
        int shortest_distance;  
        
        public Node(int num) {
            this.num = num;
            this.shortest_distance = INF;
        }
 
        @Override
        public int compareTo(Node target) {
            if (this.shortest_distance < target.shortest_distance) {
                return -1;
            } else if (this.shortest_distance > target.shortest_distance) {
                return 1
            }
            return 0;
        }
        
        @Override
        public String toString() {
            return "[" + num + "," + shortest_distance + "]";
        }
    }
}
 
 
 
 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฐฐ๋‹ฌ

5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4

programmers.co.kr