Skip to content

Commit 2d81af9

Browse files
committed
[BOJ]18352특정거리의 도시찾기 / 실버2 / 실패
https://www.acmicpc.net/status?from_mine=1&problem_id=18352&user_id=zaqquum01
1 parent 70d5b14 commit 2d81af9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
4 4 2 1 # 도시 개수 n , 도로 개수 m , 거리 정보 k , 출발 도시 번호 x
3+
1 2
4+
1 3
5+
2 3
6+
2 4 # 2~m 번 a -> b
7+
다익스트라
8+
"""
9+
import heapq
10+
#0 input
11+
INF = 20000
12+
city_count , road_count , target_km , start =map(int,input().split())
13+
14+
#1. graph 그리기 -> 인접 리스트 방식 , 최단거리 테이블
15+
graph = [[] for i in range(city_count+1) ]
16+
distance = [INF]* (city_count+1)
17+
18+
for _ in range(road_count):
19+
a,b = map(int, input().split())
20+
# a -> b ,b 는 a 에서 갈수 있는 인접한 city
21+
for j in range(len(graph)):
22+
if a == j:
23+
graph[a].append((b,1))
24+
25+
26+
# 다익스트라
27+
#1. 초기 노드 설정 #2. 최단거리 테이블 최기화
28+
q =[]
29+
distance[start]=0
30+
heapq.heappush(q, (0,start))
31+
# 2. 최단 거리(heappop) & 방문 x 노드 선택
32+
while q :
33+
dist , now = heapq.heappop(q)
34+
if distance[now] < dist :
35+
continue
36+
#4, Cost(A->X) + Cost(X->B) < Cost(A->B) : 업데이트 & push
37+
for i in graph[now]:
38+
cost = dist + i[1]
39+
if cost < distance[i[0]] :
40+
distance[i[0]] = cost
41+
heapq.heappush(q,(cost,i[0]))
42+
43+
islenK = False
44+
for i in range(1, city_count+1):
45+
if distance[i] == target_km :
46+
islenK = True
47+
print(i)
48+
if not islenK :
49+
print(-1)

0 commit comments

Comments
 (0)