-
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
48-tgyuuAn #171
Conversation
μ΄λΌλΌ |
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.
λ€μ΅μ€νΈλΌλ κ°λ
λ§ μκ³ μμκ³ , 'νλ‘μ΄λ μμ
', '벨λ§ν¬λ'λ μ²μ λ€μ΄λ΄€λ€μ γ·γ·
λμλκ³Ό νκ·λ λλΆμ μκ³ λ¦¬μ¦ νλ λ μ€μ€ ν΄κ°λλ€ !!
λ€μ΅μ€νΈλΌμμ μμ κ°μ€μΉ λλ¬Έμ 무ν루νλ₯Ό λκ² λλ λ¬Έμ κ° λ°μν΄μ λ²¨λ§ ν¬λλ₯Ό μ¬μ©νκ³ , μμ κ°μ€μΉλ§ μλ κ²½μ°μλ λ€μ΅μ€νΈλΌλ₯Ό μ¬μ©νλ κ²μ΄ λ ν¨μΈμ μ΄λ€!
μμ§ μ΄ λ¬Έμ λ₯Ό νκΈ°μ λ€μ΅μ€νΈλΌλ μ λλ‘ λͺ¨λ₯΄λ κ² κ°μμ μ μ₯ν΄λμλ€κ° λ€μ΅μ€νΈλΌ 곡λΆνκ³ λ€μ νμ΄λ³΄κ² μ΅λλ· !! π₯π₯
μλ§ μΆνλ립λλ€ λμ₯λ ππ
# μ¬μ΄ν΄μ΄ λ°μν΄λ κ²½λ‘λ κ΄λ ¨μ΄ μμ μλ μμΌλ―λ‘, | ||
# μ¬μ΄ν΄μ΄ λ°μν μ§μ μ΄ λͺ©ν μ§μ κ³Ό κ΄λ ¨μ΄ μλμ§ μ²΄ν¬ν¬ | ||
deq = deque([start]) | ||
visited = {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.
μ¬μνκ±°κΈ΄ νλ°, 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.
μ§ν©μ μ μΈν λμλ νμ΄ νλ λ°μ μμ λ μ½€λ§λ₯Ό λ£μ΄μ€μΌ ν΄μ!
μ μ½λλ
visited = set()
visited.add(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.
λλμ΄ νμμ΅λλ€
5μκ° μ°μμΌλ‘ μ‘μ λ¬Έμ λ μ΄κ² μ²μμΈκ±° κ°λ€μ
INF = -int(1e9)
N, M = map(int, input().split())
edges = [[] for _ in range(N + 1)]
for _ in range(M):
u, v, w = map(int, input().split())
edges[u].append((v, w))
dist = [INF] * (N + 1)
dist[1] = 0
path = list(range(N + 1))
for _ in range(N - 1):
for u in range(1, N + 1):
for v, w in edges[u]:
if dist[u] != INF and dist[v] < dist[u] + w:
dist[v] = dist[u] + w
path[v] = u
connected_start_to_end = False
queue = deque()
visited = set()
for u in range(1, N + 1):
for v, w in edges[u]:
if dist[u] != INF and dist[v] < dist[u] + w:
queue.append(v)
visited.add(v)
while queue:
current = queue.popleft()
if current == 1 or current == N:
connected_start_to_end = True
break
for v, w in edges[current]:
if v not in visited:
visited.add(v)
queue.append(v)
if connected_start_to_end:
print(-1)
exit()
if dist[N] == INF:
print(-1)
exit()
q = deque()
x = N
while x != path[x]:
q.appendleft(x)
x = path[x]
q.appendleft(x)
print(*q)
λ°μ μ½λκ° 76%μμ λ§νλ μ½λμμ
μκΉ μ μλ μ¬μ΄ν΄μ΄ μλλΌλ, ν΄λΉ μ¬μ΄ν΄μ΄ μμ μ§μ - λͺ©ν μ§μ κ³Ό μ°κ΄μ΄ μλ κ²½μ° μ΅μ μ κ²½λ‘λ₯Ό μ°Ύμ μ μλ€.
νκ·λ λ΄μ© 보기μ μλ μ΄ λ΄μ©μ μμ μκ°νμ§λ λͺ»νμκ³ , λ€μ μ΄κ±° μ²λ¦¬νλλ° ν 3μκ° κ±Έλ Έμ΅λλ€
edges = [list(map(int, input().split())) for _ in range(M)]
dist = [INF] * (N + 1)
dist[1] = 0
path = list(range(N + 1))
for n in range(N):
updated = False
for u, v, w in edges:
if dist[u] != INF and dist[v] < dist[u] + w:
if n == N - 1:
print(-1)
exit()
dist[v] = dist[u] + w
path[v] = u
updated = True
if not updated:
break
if dist[N] == INF:
print(-1)
exit()
q = deque([N])
x = path[N]
while x != path[x]:
q.appendleft(x)
x = path[x]
q.appendleft(1)
print(*q)
π λ¬Έμ λ§ν¬
골λͺ©κΈΈ
βοΈ μμλ μκ°
1μκ° 20λΆ
β¨ μλ μ½λ
μ΄ λ¬Έμ λ μλμ κ°μ΄ μμ κ°μ€μΉκ° μμΌλκΉ λ²¨λ§ ν¬λλ₯Ό μ¬μ©ν¨μ μ½κ² λ μ¬λ¦΄ μ μλ€.
벨λ§ν¬λμ μκ° λ³΅μ‘λλ$μ΄ λ
Έλμ μ * κ°μ μ κ°―μ$ μ΄λ―λ‘ μ΄ 200λ§μΌλ‘ μκ° λ³΅μ‘λ μμΌλ‘λ μΈμ΄ν κ°λ₯νλ€.
λ²¨λ§ ν¬λμ μμΈν ꡬν λ°©λ²μ @gjsk132 μκ² μμ..
μ€νμ λμ€μ½λ μκ³ λ¦¬μ¦ λ ΈνΈμ μ¬λΌμ¬ κ²μ΄λ€ νν.
++ κΈ μ¬λΌμμ΅λλ€. λμ€μ½λ μ±λμ μκ³ λ¦¬μ¦ λ ΈνΈ μ½μ΄λ³΄μΈμ!
λ€μ λ¬Έμ λ‘ λμμμ,
κ·Έλ¬λ©΄ μ΄λ κ² μκ°ν μ μλ€.
'μμ μ¬μ΄ν΄μ΄ λ°μνλ κ²½μ° "-1" μ μΆλ ₯νλ©΄ λκ² λ€ ...!'
'μ, μμ μ¬μ΄ν΄μ΄ λ°μνμ§ μλλΌλ κ°μ μ΄ λ§μ§λ§ λ Έλ κΉμ§ μ°κ²°λμ§ μμ κ²½μ°λ μμΌλκΉ, λ§μ§λ§ λ Έλλ‘ μ°κ²°λμ§ μμ κ²½μ°λ "-1"μ μΆλ ₯ν΄μΌκ² λ€.'
μ΄λ κ²λ§ μ¬κ³ νκ³ μ½λλ₯Ό μμ±ν΄λκ°λ€.
μ μ½λλ₯Ό μ μΆνκ³ λλ,
μ λ΅ νΌμΌνΈκ° μμ μ¬λΌκ°μ λ§μΆ μ€ μκ³ μ’μνλ€.
κ·Όλ° 76νΌμμ κ³μ κ±Έλ Έλ€.
ν .. λλ체 μ΄λκ° λ¬Έμ μλ κ±ΈκΉ..
κ·Όλ° κ³¨λ 1μΉκ³ λμ΄λκ° λ무 μ¬μ΄ κ² κ°μμ λ€μ λ¬Έμ λ₯Ό κΌΌκΌΌν μ½μ΄λ³΄μλ€.
μλ λ¬Έμ₯μ΄ λ΄ μ΄λͺ©μ λμλ€.
ν ... λΆλͺ μ΅μ μ κ²½λ‘κ° μλ κ²½μ° λ§κ³ λ λ€λ₯Έ κ²½μ°κ° μꡬλ μκ°μ νλ€.
κ·Όλ° λμ ν μ무리 λ΄λ λ μ€λ₯΄μ§κ° μλλ€.
κ·Έλμ μ§λ¬Έ κ²μνμμ λ°λ‘λ₯Ό λ€μ‘λ€.
μλΏμΈ....
"μ¬μ΄ν΄μ΄ μλλΌλ, ν΄λΉ μ¬μ΄ν΄μ΄
μμ μ§μ - λͺ©ν μ§μ
κ³Ό μ°κ΄μ΄ μλ κ²½μ° μ΅μ μ κ²½λ‘λ₯Ό μ°Ύμ μ μλ€."μ¦, μμ λ°λ‘λ₯Ό 보면 2λ²κ³Ό 3λ² λ Έλλ μ¬μ΄ν΄μ λ§λ€μ΄ 무ν 루νλ₯Ό λκΈ° λλ¬Έμ μ μμμμλ μ¬μ΄ν΄μ΄ μ‘΄μ¬νλ€κ³ ν μ μλ€.
νμ§λ§, μμ μ§μ μΈ 1λ²κ³Ό λͺ©ν μ§μ μΈ 4λ² λ Έλλ μ¬μ΄ν΄μ ν¬ν¨λμ΄ μμ§ μμΌλ―λ‘, μ΅μ μ κ²½λ‘λ₯Ό λ§λ€ μ μλ€.
κ·Έλ¬λ©΄ λ²¨λ§ ν¬λλ₯Ό μ§ννλ©΄μ, μμ μ¬μ΄ν΄μ΄ λ°μν¨μ νμ ν μκ° ν΄λΉ λ Έλλ₯Ό κΈ°μ€μΌλ‘ BFS νΉμ DFSλ₯Ό λλ €μ μ°κ΄λμ΄ μλ λ Έλλ₯Ό νμ νλ€.
μ΄ κ³Όμ μμ μμ μ§μ μΈ 1λ² λ Έλμ λͺ©ν μ§μ μΈ Nλ² λ Έλκ° ν¬ν¨λμ΄ μμΌλ©΄ λ°λ‘
-1
μ μΆλ ₯νκ³ ,ν΄λΉ λ Έλλ€μ΄ ν¬ν¨λμ§ μμμ κ²½μ° μ¬μ΄ν΄μ΄ μμ λμ κ°μ΄ μμ μ§μ λΆν° λ§μ§λ§ μ§μ κΉμ§ μ°κ²°λ μ μλ μ§ μ²΄ν¬νλ€.
λ μ§μ κΉμ§ μ΄μ΄μ§λ€λ©΄ ν΄λΉ κ²½λ‘λ₯Ό μΆλ ₯, μλ κ²½μ°
-1
μ μΆλ ₯.π μλ‘κ² μκ²λ λ΄μ©