[BOJ] 1753번: 최단경로

2023. 3. 4. 21:06·Problem Solving/Baekjoon

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

 

1753번: 최단경로

첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1 ≤ K ≤ V)가

www.acmicpc.net

 

 

1. 문제 설명 

최단 경로를 구하는 문제이다. 

 

 

2. 접근 방식 

다익스트라 알고리즘을 사용하면 쉽게 풀 수 있다 !! (기본적인 문제)

 

 

 

3. 주석 달기 (변수 설명, 각 줄마다 문장으로 설명, 함수 설명)

import java.util.*;

public class Main {
    static class Edge { // from 에서 to 정점까지의 가중치(weight)를 담는 클래스
        public int to;
        public int weight;
        public Edge(int _to, int _weight) {
            this.to = _to;
            this.weight = _weight;
        }
    }

    static class Info { // v, d 를 담는 클래스
        public int idx;
        public int dist;
        public Info(int _idx, int _dist) {
            this.idx = _idx;
            this.dist = _dist;
        }
    }
    static List<Edge>[] edges;
    static int[] dist;
    static int V, E, K;
    static Scanner sc = new Scanner(System.in);
    static void input() {
        V = sc.nextInt(); E = sc.nextInt();
        K = sc.nextInt();
        dist = new int[V+1];
        edges = new List[V+1];
        for(int i = 1; i <= V; i++) edges[i] = new ArrayList<>();
        for(int i = 1; i <= E; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            int weight = sc.nextInt();

            edges[from].add(new Edge(to, weight));
        }
    }

    static void dijkstra(int start) {
        // 1. dist 배열 초기화 및 D[S,0] 넣기
        for(int i = 1; i <= V; i++) dist[i] = Integer.MAX_VALUE;


        // 최소 힙 생성
        PriorityQueue<Info> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.dist)); // 거리(d) 순으로 정렬 최소힙

        // 시작점에 대한 정보(info)을 기록에 추가하고, 거리 배열(dist) 갱신
        pq.add(new Info(start, 0));
        dist[start] = 0;

        // 2. D가 비어있냐?
        while (!pq.isEmpty()) {

            // 3. 비어있지 않다면 min(v, d) 추출
            Info info = pq.poll();

            // 4. (v,d)가 가치가 있냐?
            if(dist[info.idx] != info.dist) continue;

            // 가치가 있다면 연결된 모든 간선들을 통해서 다른 정점들에 대한 정보 갱신
            for(Edge e : edges[info.idx]) {
                if(dist[info.idx] + e.weight >= dist[e.to]) continue;

                // 5. v, d를 통해 새로운 정보를 D에 추가
                dist[e.to] = dist[info.idx] + e.weight;
                pq.add(new Info(e.to, dist[e.to]));
            }
        }
    }
    static void pro() {
        dijkstra(K);
        for(int i = 1; i <= V; i++) {
            if (dist[i] == Integer.MAX_VALUE)
                System.out.println("INF");
            else
                System.out.println(dist[i]);
        }
    }
    public static void main(String[] args) {
        input();
        pro();
    }
}

 

4. 분석 및 시간복잡도 

시간복잡도 : O(ElogV)

 

 

 

'Problem Solving/Baekjoon' 카테고리의 다른 글
  • [BOJ] 14502번: 연구소
  • [BOJ] 14926번: Not Equal
  • [BOJ] 1326번: 폴짝폴짝
  • [BOJ] 9375번: 패션왕 신해빈
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준
    S3
    세그먼트 트리
    컨테이너
    인덱스 시그니처
    interface
    티스토리챌린지
    PrefixSum
    파이썬
    Bucket
    구간 업데이트
    구간합
    온라인 쿼리
    인덱서블 타입
    segment tree
    imos법
    누적합
    점 업데이트
    python
    알고리즘
    오프라인 쿼리
    도커
    docker image
    오블완
    AWS
    docker
    TypeScript
    삼성기출
    CORS
    타입스크립트
    인터페이스
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[BOJ] 1753번: 최단경로
상단으로

티스토리툴바