-
Notifications
You must be signed in to change notification settings - Fork 1
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
12-fnzksxl #58
12-fnzksxl #58
Conversation
if next_dest == 1 and distance < 0:
return False ์ด๋ถ๋ถ์์ ์์ ๊ฐ์ค์น๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ธ๊ฐ์!!? ๋ฌธ์ ๋ฅผ ํ์ด๋ดค๋๋ฐ ๊ทธ๋ํ ๋ง๋๋๊ฑฐ๋ถํฐ ๋ค์ ๊ณต๋ถ๋ฅผ ํด์ผํ ๊ฒ ๊ฐ๋ค์.. |
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.
๋จผ์ ์ข์ ๋ฌธ์ ๊ณต์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋ฒจ๋จผ ํฌ๋๋ฅผ ์ฒ์ ์ ํด์ ํด๋น ์๊ณ ๋ฆฌ์ฆ์ ๊ณต๋ถํ๊ณ ํ์์ต๋๋ค.
ํผ ๋ฐฉ๋ฒ์ ๋๊ฐ๋ค์
import java.io.BufferedReader
import java.io.InputStreamReader
const val INF = Long.MAX_VALUE
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (n, m) = br.readLine().split(" ").map { it.toInt() }
val edges = mutableListOf<List<Int>>().apply {
repeat(m) {
add(br.readLine().split(" ").map { it.toInt() })
}
}
val distance = Array(n + 1) { INF }
if (bellmanFord(n, 1, edges, distance)) {
for (i in 2..n) {
println(if (distance[i] != INF) distance[i] else -1)
}
return
}
println(-1)
}
private fun bellmanFord(n: Int, start: Int, edges: List<List<Int>>, distance: Array<Long>): Boolean {
distance[start] = 0
for (i in 0 until n) {
for (edge in edges) {
val (current, next, cost) = edge
if (distance[current] != INF && distance[next] > distance[current] + cost) {
distance[next] = distance[current] + cost
if (i == n - 1) {
return false
}
}
}
}
return true
}
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.
https://youtu.be/Ppimbaxm8d8
์ด ๋ถ ์ค๋ช
๋ฃ๊ณ ํ ๋ฒ ๊ณต๋ถํด๋ณด์์ด๋ค.. ์ด๋ ต๋ค์..!
import sys
input = sys.stdin.readline
INF = int(1e9)
def bellman_ford(start):
dist[start] = 0
for i in range(1,n+1):
for j in range(m):
now, next, cost = edges[j][0], edges[j][1], edges[j][2]
if dist[now] != INF and dist[next] > dist[now] + cost:
dist[next] = dist[now] + cost
if i == n :
return True
return False
n,m = map(int, input().split())
edges = []
dist = [INF] * (n+1)
for _ in range(m):
a,b,c = map(int, input().split())
edges.append((a,b,c))
negative_cycle = bellman_ford(1)
if negative_cycle :
print(-1)
else :
for i in range(2,n+1):
if dist[i] == INF:
print(-1)
else :
print(dist[i])
์ ๋ถ๋ถ์์ ์์ ๊ฐ์ค์น๋ฅผ ์ฒ๋ฆฌํ๋ค๊ณ ํด๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ๊ทผ๋ณธ์ ์ผ๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค. |
๐ ๋ฌธ์ ๋งํฌ
ํ์๋จธ์
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
์์ ๊ฐ์ค์น๊ฐ ์๋ค? ๊ทธ๋ฌ๋ฉด ๋จผ์ ๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋ ์ฌ๋ผ์ผ ํฉ๋๋ค! (๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ค์ต์คํธ๋ผ๋ ๊ฐ๋ฅ์ ํฉ๋๋ค)
์ฌ์ง์ด, ์์ ์ฌ์ดํด๋ ์ฐพ์์ผ ํ๋ ๋ฌธ์ .. ๋ฒจ๋ง-ํฌ๋๋ก ํ์ด์ผ๋ง ํ๋ ๋ฌธ์ ๊ฐ๊ตฐ์.
์ ์ง ๋ถ๊ธฐ์ ์ ์ก์ผ๋ฉด ๋ค์ต์คํธ๋ผ๋ก ๋ ๊ฒ ๊ฐ์ฃ ? ๋๋๊ณ ๋ฒจ๋ง-ํฌ๋๋ก ํ๋ผ๊ณ ํ๊ธธ๋
์ ๋ ๊ด์๋ฆฌ ์ฌ์ ์ด ๋์ ๋ค์ต์คํธ๋ผ๋ก ์ฝ๋๋ ์ง๋ดค์ต๋๋ค.
๊ทผ๋ฐ ์ด ์ฝ๋๋ ์ ๋ ์ ๋ฉ๋๋ค. ๋ช ํํ ๋ฐ๋ก๊ฐ ์์ผ๋ ํ ๋ฒ ์๊ฐํด๋ณด์ธ์!
์ธ์ ๋ค์ต์คํธ๋ผ๋ฅผ ์ฐ๋ฉด ์ ๋๋์ง ๊นจ๋ฌ์ ์ ์์ต๋๋ค.
์ ์ ๋ ์ค๋ฅด์ ๋ค๋ฉด ์๋ ค๋๋ฆฌ๊ฒ ์ด๋๋ค. ์ฐก๋๐
์ด ๋ฌธ์ ๋ ๋ฒจ๋ง-ํฌ๋์ ๊ธฐ๋ณธ์ ์ถฉ์คํ๊ฒ ๋ฐ๋ผ๊ฐ๋ฉด ๋ฐ๋ก ํ๋ฆฝ๋๋ค.
ํน์๋ ๊ธฐ์ต์ด ์ ์ ๋์๋ ๋ถ๋ค์ ์ํด ๊ฐ๋จํ๊ฒ ์ค๋ช ์ ๋๋ฆฌ์๋ฉด
๋ ธ๋์ ๊ฐ์๋งํผ ๋ชจ๋ ๊ฐ์ ์ ํ ๋ฒ ๋ฐ๋ผ๊ฐ ๋ด ๋๋ค.
๊ทผ๋ฐ ๊ทธ๋ํ ๋ด์์ ์ต์ ๊ฒฝ๋ก๋ฅผ ์ฐพ์์ ๋ ๊ทธ ๊ฒฝ๋ก๋ ์ด N-1๊ฐ์ ๊ฐ์ ์ ์ฌ์ฉํ๊ฒ ๋ ํ ๋ฐ์.
N๋ฒ์งธ ๋ฐ๋ณต์์๋ ๊ฐฑ์ ์ด ๋๋ค๋ฉด ์์ ์ฌ์ดํด์ด ์กด์ฌํ๋ค๋ ๊ฑธ ์ ์ ์๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
์ต์ด ์ํ์ ๋๋ค. ํ๋ฆ์ ๋ฐ๋ผ์ค์๊ธฐ ํธํ๋๋ก ๊ณ์ฐ์ด ๋๋ ์์๋ฅผ ์์ ์ ์ด๋์ต๋๋ค.
๋ชจ๋ ๊ฐ์ ๋ค์ ์ํํด์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํด์ค๋๋ค.
N๋ฒ ์งธ ๋ผ์ด๋์ธ R3์ ๊ฐ๊ณผ R2์ ๊ฐ์ด ๋๊ฐ์ฃ ? ๊ฐฑ์ ์ด ์ผ์ด๋์ง ์์๋ค๋ ๊ฒ์ ์์ ์ฌ์ดํด์ด ์๋ค๋ ๋ป์ ๋๋ค.
์ต์ด ์ํ์ ๋๋ค.
์ด? ์์ํ ๋์๊ฐ ๋๋ ์ซ์๋ค๋ก ๊ฑฐ๋ฆฌ๊ฐ ๋ฐ๋์๊ตฐ์.
์์ ์ฌ์ดํด์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด, ์ฌ๊ธฐ์ ์๋ก ๊ฐฑ์ ์ด ์์ด์ผ ํ์ง๋ง ๊ฐฑ์ ์ด ์ผ์ด๋ฉ๋๋ค.
๊ฐฑ์ ์ด ๋ฐ์ํ๋ค๋ ๊ฒ์? ์์ ์ฌ์ดํด์ด ์๋ค๋ ๊ฒ์ด์ฃ .
์ ์ฒด ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ ๋๋๊ฑด ์ ๋๋ ๊ฑฐ๋ค.