ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준]1240: 노드사이의 거리 - JAVA
    문제풀이/백준 2021. 3. 28. 16:28

    [백준]1240: 노드사이의 거리

    www.acmicpc.net/problem/1240

     

    1240번: 노드사이의 거리

    N(2≤N≤1,000)개의 노드로 이루어진 트리가 주어지고 M(M≤1,000)개의 두 노드 쌍을 입력받을 때 두 노드 사이의 거리를 출력하라.

    www.acmicpc.net

    풀이

    두 노드 사이의 최단 경로를 반환하는 문제였다. 다양한 방법이 있겠지만 나는 다익스트라 알고리즘을 사용하여 풀었다. 노드와 간선 가중치의 정보를 입력 받은 후 M개의 개수 만큼 다익스트라 알고리즘을 돌려 M개의 거리를 출력하였다.

     

    다익스트라 알고리즘은 우선순위 큐(최소힙)와 dist배열, visited배열을 사용하여 구현해 주었다. 기본적인 다익스트라 알고리즘을 구현할 수 있는지 묻는 수준의 문제였고 어렵지 않게 풀었다.

     

    코드

    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
    import java.util.*;
     
    public class Main {    
     
        static int n, m;
        static ArrayList<Node>[] list;
        static int[] dist;
        static boolean[] visited;
        
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            
            n = scan.nextInt();
            m = scan.nextInt();
            
            list = new ArrayList[n + 1];
            for(int i = 1; i <= n; i++) {
                list[i] = new ArrayList<>();
            }
            
            for(int i = 0; i < n - 1; i++) {
                int s = scan.nextInt();
                int d = scan.nextInt();
                int v = scan.nextInt();
                list[s].add(new Node(d, v));
                list[d].add(new Node(s, v));
            }
            
            for(int i = 0; i < m; i++) {
                int start = scan.nextInt();
                int end = scan.nextInt();
                dist = new int[n + 1];
                Arrays.fill(dist, Integer.MAX_VALUE);
                visited = new boolean[n + 1];
                dijkstra(start, end);
                System.out.println(dist[end]);
            }
        }    
        
        public static void dijkstra(int s, int e) {
            PriorityQueue<Node> q = new PriorityQueue<>();
            dist[s] = 0;
            q.offer(new Node(s, 0));
            
            while(!q.isEmpty()) {
                Node current = q.poll();
                
                if(visited[current.n] == false) visited[current.n] = true;
                else continue;
                
                for(int i = 0; i < list[current.n].size(); i++) {
                    Node next = list[current.n].get(i);
                    if(dist[next.n] > dist[current.n] + next.v) {
                        dist[next.n] = dist[current.n] + next.v;
                        q.offer(new Node(next.n, dist[next.n]));
                    }
                }
            }
        }
        
        public static class Node implements Comparable<Node> {
            int n;
            int v;
            
            public Node(int n, int v) {
                this.n = n;
                this.v = v;
            }
            
            @Override
            public int compareTo(Node node) {
                return this.v - node.v;
            }
        }
    }
     
    cs

    댓글

Designed by Tistory.