-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from AlgoLeadMe/48-tgyuuAn
48-tgyuuAn
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import sys | ||
from collections import deque | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N, M = map(int, input().split()) | ||
edge = [[] for _ in range(N+1)] | ||
|
||
# κ°μ μ 보 λ°μ | ||
for _ in range(M): | ||
start, destination, cost = map(int,input().split()) | ||
edge[start].append([destination, cost]) | ||
|
||
# μ΄κΈ° μΈν | ||
board = [-int(1e9) for _ in range(N+1)] | ||
board[1] = 0 | ||
|
||
# μ΅μ μ κ²½λ‘λ₯Ό μ°ΎκΈ° μν΄ μμΆμ νκΈ° μν΄μ μ΄μ λ Έλλ₯Ό κΈ°λ‘ | ||
prev_node = [-1 for _ in range(N+1)] | ||
prev_node[1] = 0 | ||
|
||
for _ in range(N-1): | ||
for start in range(1,N+1): | ||
for destination, cost in edge[start]: | ||
if board[destination] < board[start] + cost: | ||
board[destination] = board[start] + cost | ||
prev_node[destination] = start | ||
|
||
has_cycle = False | ||
is_connect_target = False | ||
for start in range(1,N+1): | ||
for destination, cost in edge[start]: | ||
# μ¬μ΄ν΄ λ°μ | ||
if board[destination] < board[start] + cost: | ||
has_cycle = True | ||
|
||
# μ¬μ΄ν΄μ΄ λ°μν΄λ κ²½λ‘λ κ΄λ ¨μ΄ μμ μλ μμΌλ―λ‘, | ||
# μ¬μ΄ν΄μ΄ λ°μν μ§μ μ΄ λͺ©ν μ§μ κ³Ό κ΄λ ¨μ΄ μλμ§ μ²΄ν¬ν¬ | ||
deq = deque([start]) | ||
visited = {start,} | ||
while deq: | ||
now = deq.popleft() | ||
|
||
for d, c in edge[now]: | ||
if d in visited: continue | ||
|
||
deq.append(d) | ||
visited.add(d) | ||
|
||
# μ¬μ΄ν΄μ΄ μκ³ λͺ©νμ§μ νΉμ μμμ§μ κ³Ό λΆμ΄μμΌλ©΄ -1 | ||
if d == 1 or d == N: | ||
is_connect_target = True | ||
break | ||
|
||
if is_connect_target: break | ||
break | ||
|
||
# μ¬μ΄ν΄μ΄ μλλ° ν΄λΉ μ¬μ΄ν΄μ΄ λͺ©νμ μ°κ²°λμ΄ μμ κ²½μ° | ||
if has_cycle and is_connect_target: print(-1) | ||
else: | ||
answer = [] | ||
now = N | ||
while now != 1: | ||
answer.append(now) | ||
now = prev_node[now] | ||
|
||
answer.append(now) | ||
|
||
if now != 1: print(-1) | ||
else: print(*answer[::-1]) | ||
|
||
# μ΄ κ°μ = 2λ§κ°, | ||
# μ΄ λ Έλ = 100κ° | ||
# λ²¨λ§ ν¬λ = ( κ°μ X λ Έλ -1 ) -> 198λ§ μκ° λ³΅μ‘λ κ°λ₯. | ||
|
||
# μ΅μ μ κ²½λ‘ | ||
# μ¬μ΄ν΄μ΄ λ°μν΄λ κ° μ μμ μ μμ. | ||
# μ¬μ΄ν΄μ΄ μλλΌλ λλ¬ν μ μμ μ μμ. |