-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
48-tgyuuAn #171
Merged
Merged
48-tgyuuAn #171
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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λ§ μκ° λ³΅μ‘λ κ°λ₯. | ||
|
||
# μ΅μ μ κ²½λ‘ | ||
# μ¬μ΄ν΄μ΄ λ°μν΄λ κ° μ μμ μ μμ. | ||
# μ¬μ΄ν΄μ΄ μλλΌλ λλ¬ν μ μμ μ μμ. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ¬μνκ±°κΈ΄ νλ°, start λ€μ ,<< μ΄κ±΄ λ³ μλ―Έ μλ 건κ°μ? π€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ§ν©μ μ μΈν λμλ νμ΄ νλ λ°μ μμ λ μ½€λ§λ₯Ό λ£μ΄μ€μΌ ν΄μ!
μ μ½λλ
λ λμΌνκ² μλν©λλ€!