Skip to content

Commit b64a7a3

Browse files
committed
0708
1 parent e86fd15 commit b64a7a3

File tree

3 files changed

+124
-76
lines changed

3 files changed

+124
-76
lines changed

.idea/workspace.xml

Lines changed: 79 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solve.ac/class 4/1504_특정한 최단 경로/1504_특정한 최단 경로.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def dikjstra(start):
2828
grape = [[] for _ in range(N + 1)]
2929
for _ in range(E):
3030
s, e, v = map(int, input().split())
31+
#방향성이 없는 그래프이면 반대의 경우도 넣어야한다.
3132
grape[s].append((e, v))
3233
# 한번 이동했던 정점은 물론, 한번 이동했던 간선도 다시 이동할 수 있다
3334
grape[e].append((s, v))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 벨만-포드 알고리즘을 이용해 해결하였다.
2+
#
3+
# 시간이 줄어들면서 출발 위치로 돌아오는 것 을 보고 벨만포드를 떠올리기
4+
# 도로는 무방향이므로 양방향 간선 2개를 추가하기
5+
# 웜홀의 가중치는 음수로 취급하기
6+
7+
8+
9+
def BellmanFord(start):
10+
distance[start] = 0
11+
for k in range(1,N+1):
12+
for i in range(1,N+1):
13+
for next, cost in graph[i]:
14+
# 현재 노드에 도달이 가능하면서
15+
# 다음 노드로 이동하는 거리가 최단거리로 갱신가능한 경우
16+
if distance[next] > distance[i] + cost:
17+
distance[next] = cost + distance[i]
18+
if k == N:
19+
return True
20+
return False
21+
22+
INF = int(1e9)
23+
24+
TC = int(input())
25+
for _ in range(TC):
26+
N, M, W = map(int, input().split())
27+
graph = [[] for _ in range(N + 1)]
28+
distance = [INF] * (N + 1)
29+
for _ in range(M):
30+
S, E, T = map(int, input().split())
31+
##방향성이 없는 그래프이면 반대의 경우도 넣어야한다.
32+
graph[S].append((E, T))
33+
graph[E].append((S, T))
34+
35+
for _ in range(W):
36+
WS, WE, WT = map(int, input().split())
37+
graph[WS].append((WE, -WT))
38+
39+
40+
result=BellmanFord(1)
41+
if result:
42+
print("YES")
43+
else:
44+
print("NO")

0 commit comments

Comments
 (0)