Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- auto 키워드
- 값/참조/주소에 의한 전달
- 알고리즘 조건
- 문자형 배열
- 문제해결 단계
- 프로그래머스 배열만들기4
- 주기억장치
- C언어 계산기 프로그램
- LAN의 분류
- c언어 괄호검사
- 원형 연결 구조 연결된 큐
- 프로그래머스 푸드 파이트 대회
- getline()함수
- 네트워크 결합
- IPv4 주소체계
- const l-value참조자
- const화
- string유형
- C언어 스택 연산
- C언어 덱
- 입출력 관리자
- 괄호 검사 프로그램
- 유형 변환
- r-value참조자
- l-value참조자
- 백준 파이썬
- 범위 기반 for문
- 운영체제 기능
- 회전 및 자리 이동 연산
- 논리 연산
Archives
- Today
- Total
chyam
[백준] 1916번,python - 최소비용 구하기 본문

https://www.acmicpc.net/problem/1916
import heapq
n = int(input())
m = int(input())
graph = [[] for i in range(n+1)]
for i in range(m):
u,v,w = map(int,input().split())
graph[u].append([v,w])
start, end = map(int,input().split()) # 출발점, 도착점
distance = [float('inf')] * (n+1) # 각 최소 거리 저장
distance[start] = 0
q = []
heapq.heappush(q,(0,start))
while q:
x, y = heapq.heappop(q) # 비용, 기준점
if distance[y] < x: # 비용이 원래보다 작다면 필요없음
continue
for g in graph[y]: # 그래프에 있는거
cost = x + g[1]
if distance[g[0]] > cost: # 원래 비용보다 크면 갱신
distance[g[0]] = cost
heapq.heappush(q,(cost,g[0]))
print(distance[end])
'백준' 카테고리의 다른 글
| [백준] 12865번,python - 평범한 배낭(배낭 문제) (0) | 2026.01.20 |
|---|---|
| [백준] 2468번,python - 안전 영역 (0) | 2026.01.19 |
| [백준] 3078번, python - 좋은 친구 (0) | 2026.01.07 |
| [백준] 1753번,python - 최단 경로 (0) | 2026.01.06 |
| [백준] 1654번, python - 랜선 자르기 (0) | 2025.12.26 |