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 |
29 | 30 |
Tags
- 다이나믹 프로그래밍
- 트리
- 백트래킹
- 자료구조
- BFS
- 재귀
- DFS
- 서브쿼리
- 에라토스테네스의 체
- 분할정복
- 우선순위큐
- 브루트포스
- DP
- 크루스칼
- 그래프 이론
- 다익스트라
- 플로이드-워셜
- 구현
- join
- 해시
- 수학
- MST
- 다시
- 시뮬레이션
- 다이나믹프로그래밍
- GROUP BY
- 그리디
- 누적합
- 그래프 탐색
- 투포인터
Archives
- Today
- Total
기록하고 까먹지 말기
10282 본문
날짜 : 2022. 12. 10
사용 언어 : python
문제
코드
import sys
import heapq
t = int(sys.stdin.readline()) # 테스트케이스
INF = int(1e9)
def dijkstra(start):
q = []
heapq.heappush(q, (0, start))
dist[start] = 0
while q:
d, now = heapq.heappop(q) # now까지의 길이, 현재노드(now)
if d > dist[now]: continue # 이미 확인완료
for i in graph[now]:
cost = d + i[1]
if cost < dist[i[0]]:
dist[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
return
for _ in range(t):
n, d, c = map(int, sys.stdin.readline().split()) # 노드 개수, 엣지 개수, 해킹당한 컴퓨터(start)
graph = [[] for i in range(n + 1)]
dist = [INF] * (n + 1)
for _ in range(d):
a, b, s = map(int, sys.stdin.readline().split()) # 도착, 시작, cost
graph[b].append((a, s)) # 시작노드에 대한 (도착점, cost)
dijkstra(c)
cnt, time = 0, 0
for i in dist:
if i == INF: continue
cnt += 1
time = max(time, i)
print(cnt, time)
#print(dist)
풀이
- 다익스트라를 활용한 풀이이다.
알게된 점
- 이번엔 이전 다익스트라 알고리즘을 참고하지 않고 풀이했다.
참고 사이트
-