HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • back-end
  • 알고스팟
  • Database
  • 백엔드
  • 코딩
  • 개발자
  • Algospot
  • 데이터베이스
  • programmers
  • 알고스팟Python
  • 백준Go
  • 프로그래머스Python
  • Baekjoon Online Judge
  • 프로그래머스
  • 프로그래밍
  • 백준Python
  • 백준C++
  • 백준
  • 프로그래머스C++
  • front-end

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[Baekjoon Online Judge] 백준 1753번 최단경로(Python)
Deprecated

[Baekjoon Online Judge] 백준 1753번 최단경로(Python)

2020. 6. 9. 18:40

© 2020 All Rights Reserved. 주식회사 스타트링크

[Baekjoon Online Judge] 백준 1753번 최단경로

(Python)

(글쓴날 : 2020.06.09)

 


* Baekjoon Online Judge, 백준 1753번 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

백준 1753번 최단경로


1) 문제

문제 링크 : 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


2) 풀이 과정

* 시간 복잡도 : O(E log V)

 

방향 그래프와 시작점이 주어질 때, 주어진 시작점에서 다른 모든 정점으로의 최단 경로를 구하는 문제입니다.

 

저의 경우, 다익스트라를 적용하였고, Python을 사용했습니다.

우선, 문제에서 주어지는 정점, 간선, 가중치를 인접 리스트로 구현한 뒤, 시작점에 다익스트라를 적용하여 문제를 해결했습니다.


3) 코드

 

* Python 코드

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
import sys, heapq
input = sys.stdin.readline
 
def BFS(hq):
    global distance
 
    while(hq):
        wei, vec = heapq.heappop(hq)
 
        if distance[vec] > wei:
            distance[vec] = wei
 
            for v, w in adjList[vec]:
                heapq.heappush(hq, (w+wei,v))
 
V, E = map(int,input().split())
INF = 2147483647
distance = [INF] * (V+1)
K = int(input())
distance[K] = 0
adjList = [[] for _ in range(V+1)]
 
for _ in range(E):
    a,b,w = map(int,input().split())
    adjList[a].append((b,w))
 
hq = []
 
for v, w in adjList[K]:
    heapq.heappush(hq, (w,v))
 
BFS(hq)
 
for d in distance[1:]:
    if d ==INF:
        print("INF")
        continue
    print(d)

 

저작자표시 비영리 변경금지

'Deprecated' 카테고리의 다른 글

[Baekjoon Online Judge] 백준 2468번 안전 영역(Python)  (0) 2020.06.10
[Baekjoon Online Judge] 백준 1916번 최소비용 구하기(Python)  (0) 2020.06.09
[programmers] 프로그래머스 예산 Level 3(Python)  (0) 2020.06.07
[programmers] 프로그래머스 정수 삼각형(Python)  (0) 2020.06.07
[programmers] 프로그래머스 카카오프렌즈 컬러링북(C++)  (0) 2020.06.07
    'Deprecated' 카테고리의 다른 글
    • [Baekjoon Online Judge] 백준 2468번 안전 영역(Python)
    • [Baekjoon Online Judge] 백준 1916번 최소비용 구하기(Python)
    • [programmers] 프로그래머스 예산 Level 3(Python)
    • [programmers] 프로그래머스 정수 삼각형(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바