- ๋ชฉํ -
๋ค์ต์คํธ๋ผ๋?
์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ํ ์ข ๋ฅ๋ก ์ ์ ๋ค ์ฌ์ด์ ๊ฐ์ค์น๋ฅผ ๋น๊ตํ๋ ๊ณผ์ ์ ํตํด ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ค.
ํด๋น ๊ทธ๋ฆผ์์ ์ ์ ๊ณผ ๊ฐ์ ์ ๊ฐ์ค์น๋ฅผ ํ๋ก ๋ํ๋ด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
ํน์ ํ์์ ํน์ ์ด๋ก ์ด๋ํ ๋ ๋ฐ์ํ๋ ๊ฐ์ค์น๋ฅผ ๋ํ๋ธ ํ์ด๋ค.
์ธ์ ํ์ง ์์ ๊ฒฝ์ฐ INF, ๋ฐฉํฅ์ฑ์ ์ํด ๋๋ฌํ ์ ์์ ๋๋ X๋ก ๋ํ๋ด์๋ค.
A(์ฌ๊ธฐ๋ก) | B | C | D | E | F | |
A(์ฌ๊ธฐ์์) | 0 | 10 | 15 | INF | INF | INF |
B | X | 0 | INF | 12 | INF | 15 |
C | X | INF | 0 | INF | 10 | INF |
D | INF | X | INF | 0 | INF | 1 |
E | INF | INF | X | INF | 0 | X |
F | INF | X | INF | X | 5 | 0 |
(๋ฐฉ๋ฌธํ ๋ ธ๋๋ ๋ ธ๋์์ผ๋ก ํ์)
1. A์ ์ธ์ ํ B, C์์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
A | B | C | D | E | F |
0 | 10 | 15 | INF | INF | INF |
2. B์ ์ธ์ ํ D, F์์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
A | B | C | D | E | F |
0 | 10 | 15 | 22 | INF | 27 |
3. C์ ์ธ์ ํ E์์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
A | B | C | D | E | F |
0 | 10 | 15 | 22 | 25 | 27 |
4. D์ ์ธ์ ํ F์์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค. (27์์ 23์ผ๋ก ๊ฐฑ์ )
A | B | C | D | E | F |
0 | 10 | 15 | 22 | 25 | 23 |
5. F์ ์ธ์ ํ E์์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค. (๋ชจ๋ ์ ์ ์ ๋ฐฉ๋ฌธ)
A | B | C | D | E | F |
0 | 10 | 15 | 22 | 25 | 23 |
1. ๋ค์ต์คํธ๋ผ
์ต์ํ์ ์ด์ฉํ์๊ณ ๋ชจ๋ ์ ์ ์ ๋ฐฉ๋ฌธํ๋ฉด ๊ฐฑ์ ๊ณผ์ ์ ์ข ๋ฃํ๋ค. ๋จ์ํ ์ต๋จ ๊ฒฝ๋ก์ ๊ฐ์ ์ถ๋ ฅํ๋ ๊ฒ์ด ์๋ ์ต๋จ ๊ฒฝ๋ก ๊ณผ์ (?)์ ์ถ๋ ฅํด์ผ ํ๋ฏ๋ก ์ต๋จ ๊ฒฝ๋ก๋ก ๊ฐฑ์ ๋ ๋ ๋ฐฉ๋ฌธํ๋ ์ ์ ์ ์๋ก์ด dictํ ๋ณ์์ ์ ์ฅํ๋ค.
import heapq
def dijkstra(v_dic, v_list, V, E, src, dst):
INF = float('inf')
distance = {}
for i in range(V):
distance[v_list[i]] = INF
distance[src] = 0
queue = []
prev = {}
for i in range(V):
prev[v_list[i]] = []
found = {}
for i in range(V):
found[v_list[i]] = False
heapq.heappush(queue, [0, src])
while queue:
v = heapq.heappop(queue)
found[v[1]] = True
for node_weight in v_dic[v[1]]:
if (found[node_weight[0]]):
continue
if (distance[node_weight[0]] > distance[v[1]] + node_weight[1]):
distance[node_weight[0]] = distance[v[1]] + node_weight[1]
heapq.heappush(queue, [distance[node_weight[0]], node_weight[0]])
prev[node_weight[0]] = [v[1], node_weight[0], node_weight[1]]
return prev
def main():
V, E = map(int, input().split())
v_list = input().split()
v_dic = {}
for i in v_list:
v_dic[i] = []
for _ in range(E):
i, j, c = input().split()
c = int(c)
v_dic[i].append([j,c])
src, dst = input().split()
prev = dijkstra(v_dic, v_list, V, E, src, dst)
prev_list = []
prev_e = dst
while prev_e != src:
prev_list.append(prev[prev_e])
prev_e = prev[prev_e][0]
prev_list.reverse()
for i in prev_list:
print(*i)
if __name__ == "__main__":
main()
2. ๋ฒจ๋ง ํฌ๋
๋ค์ต์คํธ๋ผ์ ๊ฐ์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ํ์ง๋ง ๋ค์ต์คํธ๋ผ์์ ์ฐจ์ด์ ์ ๋ค์ต์คํธ๋ผ๋ ์์ ๊ฐ์ค์น๊ฐ ์๊ณ ๋ฒจ๋ง ํฌ๋๋ ์์ ๊ฐ์ค์น๊ฐ ์ฃผ์ด์ง๋ค๋ ์ ์ด๋ค. ํด๊ฒฐ ๊ณผ์ ์ ๋น์ทํ๋ค. ๋ฒจ๋ง ํฌ๋์ ๊ฒฝ์ฐ (์ ์ ์ ์ - 1) ๊น์ง ํ์์ ํ๊ณ ์ดํ ํ๋ฒ ๋ ํ์์ ์งํํ๋ค. ์ด ๋ ์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐฑ์ ๋๋ ๊ฐ์ด ์์ผ๋ฉด ์์ ๋ฃจํ๊ฐ ์กด์ฌํ๋ค๋ ์๋ฏธ์ด๋ฏ๋ก ๋ฌธ์ ์์ ์ฃผ์ด์ง Negative Cycle!์ ์ถ๋ ฅํ๋ค.
import heapq
def bellman_ford(v_dic, v_list, V, E, src, dst):
INF = float('inf')
distance = {}
for i in v_list:
distance[i] = INF
distance[src] = 0
predecessor = {}
for i in v_list:
predecessor[i] = None
for i in range(V-1):
for i in v_dic:
for j, c in v_dic[i]:
if distance[j] > distance[i] + c:
distance[j] = distance[i] + c
predecessor[j] = [i, j, c]
for i in v_dic:
for j, c in v_dic[i]:
if distance[j] > distance[i] + c:
return -1
return predecessor
def main():
V, E = map(int, input().split())
v_list = input().split()
v_dic = {}
for i in v_list:
v_dic[i] = []
for _ in range(E):
i, j, c = input().split()
c = int(c)
v_dic[i].append([j,c])
src, dst = input().split()
predecessor = bellman_ford(v_dic, v_list, V, E, src, dst)
if predecessor == -1:
print("Negative Cycle!")
else:
prev_list = []
prev_e = dst
while prev_e != src:
prev_list.append(predecessor[prev_e])
prev_e = predecessor[prev_e][0]
prev_list.reverse()
for i in prev_list:
print(*i)
if __name__ == "__main__":
main()
3. ๊ฒฝ์ฐฐ์
๋ค์ต์คํธ๋ผ๋ฅผ ํ์ฉํ๋ค. ์์ ์ ์ ๊ณผ ๋์ฐฉ ์ ์ ์ (A, A) ~ (F, F)๋ก ๋๊ณ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๊ณ ์ด ๊ฐ์ด ์ฃผ์ด์ง ๊ฑฐ๋ฆฌ๋ณด๋ค ์์ ๊ฒฝ์ฐ ์๋ก์ด dictํ ๋ณ์์ ์ ์ฅํ๋ค. ์ด ๊ณผ์ ์ด ์ข ๋ฃ๋๊ณ dictํ ๋ณ์์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํด์ ์ด ๊ฐ์ด ๊ฐ์ฅ ํฐ ์ ์ ์ด ๊ฒฝ์ฐฐ์ด ๋ค๋ค๋ฅผ์ ์๋ ์ง์ ์ด ๊ฐ์ฅ ๋ง์ ์ ์ ์ด๋ค.
import heapq
def dijkstra(v_dic, v_list, V, E, src, dst):
INF = float('inf')
distance = {}
for i in range(V):
distance[v_list[i]] = INF
distance[src] = 0
queue = []
prev = {}
for i in range(V):
prev[v_list[i]] = []
found = {}
for i in range(V):
found[v_list[i]] = False
heapq.heappush(queue, [0, src])
while queue:
v = heapq.heappop(queue)
found[v[1]] = True
for node_weight in v_dic[v[1]]:
if (found[node_weight[0]]):
continue
if (distance[node_weight[0]] > distance[v[1]] + node_weight[1]):
distance[node_weight[0]] = distance[v[1]] + node_weight[1]
heapq.heappush(queue, [distance[node_weight[0]], node_weight[0]])
prev[node_weight[0]] = [v[1], node_weight[0], node_weight[1]]
return distance[dst]
def main():
V, E = map(int, input().split())
v_list = input().split()
v_dic = {}
for i in v_list:
v_dic[i] = []
for _ in range(E):
i, j, c = input().split()
c = int(c)
v_dic[i].append([j,c])
v_dic[j].append([i,c])
len_dic = {}
for i in v_list:
len_dic[i] = []
min_len = int(input())
for i in v_list:
for j in v_list:
result = dijkstra(v_dic, v_list, V, E, i, j)
if min_len >= result:
len_dic[i].append(result)
len_dic[j].append(result)
result = []
for i in range(len(len_dic)):
result.append([v_list[i],len_dic[v_list[i]]])
result = sorted(result, key = lambda x : len(x[1]))
print(result[len(result)-1][0], int(len(result[len(result)-1][1])//2))
if __name__ == "__main__":
main()
- ๊นํ๋ธ -
github.com/k906506/2020_Winter_Assemble-And-selfcode
'โน๏ธ ๋ผ์ดํ > 2020 ๊ฒจ์ธ๋ฐฉํ ๋ชจ๊ฐ์ฝ(๊ฐ์ธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.20(์) - 5์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2021.01.22 |
---|---|
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.20(์) - 5์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.20 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.13(์) - 4์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.13 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2020.01.05(ํ) - 3์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2021.01.05 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.05(ํ) - 3์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.05 |