일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Set
- dfs
- 자바
- 위클리 챌린지
- Zip
- 재귀함수
- BFS
- backjoon
- 추석맞이 코딩챌린지
- divmod
- 파이썬
- 다익스트라
- 정렬
- lambda
- 동적 계획법
- python
- DateTime
- programmers
- KAKAO BLIND RECRUITMENT
- heapq
- Re
- 그리디
- 정규식
- 카카오
- 수학
- 이분탐색
- Combinations
- java
- 프로그래머스
- 백준
Archives
- Today
- Total
상상쓰
[백준] 최소비용 구하기 본문
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net
출발 도시와 도착 도시가 같은 버스가 있을 수 있다는 생각을 처음에 못 했다. 그래서 코드를 추가했고 비용이 0인 버스가 있어서 fee = {} 비어있을 때랑 값이 있을 때 나눠서 다익스트라 알고리즘으로 풀었다.
import sys
from collections import defaultdict, deque
N = int(sys.stdin.readline())
M = int(sys.stdin.readline())
bus = [list(map(int, sys.stdin.readline().split())) for i in range(M)]
start, end = map(int, sys.stdin.readline().split())
dic = defaultdict(list)
fee = {}
queue = deque()
for x, y, d in bus:
dic[x].append((y, d))
fee[start] = 0
for y, d in dic[start]:
if y not in fee:
fee[y] = d
if y != end:
queue.append(y)
else:
if fee[y] > d:
fee[y] = d
while queue:
x = queue.popleft()
for y, d in dic[x]:
if y not in fee:
fee[y] = fee[x] + d
if y != end:
queue.append(y)
else:
if fee[y] > fee[x] + d:
fee[y] = fee[x] + d
if y != end:
queue.append(y)
print(fee[end])
'Coding Test' 카테고리의 다른 글
[백준] 1, 2, 3 더하기 4 (0) | 2021.11.12 |
---|---|
[백준] 줄 세우기 (0) | 2021.11.10 |
[백준] 부분합 (0) | 2021.11.08 |
[프로그래머스] 구명보트 (0) | 2021.11.02 |
[백준] 멀티탭 스케줄링 (0) | 2021.11.01 |
Comments