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 | 31 |
Tags
- 티스토리챌린지
- url 랜더링
- web-view
- 블로그 뉴비
- 훈수 가능
- 부산 맛집 OPEN API
- 프로그래머스
- 코딩테스트
- API 활용 신청
- 사업계획서
- 고고학 최고의 발견
- redux state값 유지
- React
- Redux
- python
- Dreamhack
- 공공데이터 포털
- php-1
- 오블완
- apk 빌드
- level3
- expo
- 창업 300
- 꿀팁 환영
- 새로고침
- 보안
- react-router-dom
- 드림핵
- 개발
Archives
- Today
- Total
1223v
[BOJ] Python 백준 연예인은 힘들어(17270) 본문
https://www.acmicpc.net/problem/17270
다익스트라를 통한 최단거리 비교 문제이다.
여러 조건에 맞추어 진행해야하는 다중 조건 다익스트라 문제이다
import sys
import heapq
input = sys.stdin.readline
V, M = map(int,input().split())
graph = [[] for _ in range(V+1)]
visited = [False] * (V+1)
for _ in range(M):
start, end, cost = map(int,input().split())
graph[start].append((end,cost))
graph[end].append((start,cost))
J,S = map(int,input().split())
def dijkstra(start):
hq = []
distance = [float('inf')] *(V+1)
distance[start] = 0
visited[start] = True
heapq.heappush(hq,(0, start))
while hq:
cost, now = heapq.heappop(hq)
if distance[now] < cost:
continue
for next_node, dist in graph[now]:
if distance[next_node] > cost + dist:
distance[next_node] = cost + dist
heapq.heappush(hq,(dist + cost, next_node))
return distance
# J < S
J_distance = dijkstra(J)
S_distance = dijkstra(S)
total_distance = float('inf')
J_position = float('inf')
result = -1
# 순서 정렬
for i in range(1,V+1):
if total_distance > J_distance[i] + S_distance[i] and i != J and i!= S:
total_distance = J_distance[i] + S_distance[i]
for i in range(V,0,-1):
if i != J and i != S and J_distance[i] < S_distance[i] and J_distance[i] + S_distance[i] == total_distance and J_distance[i] <= J_position:
J_position = J_distance[i]
result = i
print(result)
회고,
지헌이가 성하보다 늦으면 안된다라는 조건에서 동시에 도착하는 경우를 생각하지 못해 틀렸었다...
조건을 꼼꼼히 더 확인해야겠다.
728x90
'PS' 카테고리의 다른 글
[Programmers] Python 프로그래머스 양과 늑대(92343) (0) | 2025.01.22 |
---|---|
[BOJ] Python 백준 특정한 최단 경로(17270) (0) | 2025.01.20 |
[BOJ] Python 백준 좋다(1253) (0) | 2025.01.16 |
[BOJ] Python 백준 네트워크 복구(2211) (0) | 2025.01.16 |
[Programmers] Python 프로그래머스 가사 검색(60060) (1) | 2025.01.14 |