Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 이코테/09 최단 경로/다익스트라.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def dijkstra(start):

# 시작 노드로 가는 비용 0
heapq.heappush(q, (0, start))
# 근데 큐에 넣는 것 만으로도 distance에 저장이 되나?
distance[start] = 0

while q: # q가 비어있지 않다면,
# 최단 거리가 짧은 노드 꺼내기
# 거리, 지금 노드
dist, now = heapq.heappop(q)

# 이미 처리된 적 있는가?
Expand Down
28 changes: 28 additions & 0 deletions 이코테/09 최단 경로/미래_도시/유진.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
INF = int(1e9)
n, m = map(int, input().split())

graph = [[INF] * (n + 1) for _ in range(n + 1)]

# 자기 자신 0
for a in range(1, n + 1):
for b in range(1, n + 1):
if a == b:
graph[a][b] = 0

for i in range(m):
a, b = map(int, input().split())
graph[a][b] = 1
graph[b][a] = 1 # 안썼었음

x, k = map(int, input().split())

for i in range(1, m + 1):
for a in range(1, m + 1):
for b in range(1, m + 1):
graph[a][b] = min(graph[a][b], graph[a][i] + graph[i][b])

distance = graph[1][k] + graph[k][x]
if distance == INF:
print(-1)
else:
print(distance)
69 changes: 45 additions & 24 deletions 이코테/09 최단 경로/전보/유진.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import heapq, sys
input = sys.stdin.readline
INF = int(1e9)
n, m = map(int, input().split())

graph = [[INF] * (n + 1) for _ in range(n + 1)]

# 자기 자신 0
for a in range(1, n + 1):
for b in range(1, n + 1):
if a == b:
graph[a][b] = 0
n, m, c = map(int, input().split())
graph = [[] for i in range(n + 1)]
distance = [INF] * (n + 1)

# 간선들의 정보 입력받기
for i in range(m):
a, b = map(int, input().split())
graph[a][b] = 1
graph[b][a] = 1 # 안썼었음

x, k = map(int, input().split())

for i in range(1, m + 1):
for a in range(1, m + 1):
for b in range(1, m + 1):
graph[a][b] = min(graph[a][b], graph[a][i] + graph[i][b])

distance = graph[1][k] + graph[k][x]
if distance == INF:
print(-1)
else:
print(distance)
x, y, z = map(int, input().split())
graph[x].append((y, z))

# 다익스트라
def dijkstra(start):
q = [] # 우선순위 큐

heapq.heappush(q, (0, start))
# 시작지점 거리 0
distance[start] = 0

while q:
dist, now = heapq.heappop(q)

# 이미 처리했다면,
if distance[now] < dist:
continue

# 지금 위치에서 비용계산을 하면
for i in graph[now]:
cost = dist + i[1]

# 다른 노드를 거치는게 더 적게 걸린다면,
if cost < distance[i[0]]:
heapq.heappush(q, (cost, i[0]))
dijkstra(c) # 다익스트라 함수 실행

# C와 연결된 도시 갯수 세기
count = 0

# 전보가 가는 시간 측정
# -> 총 걸리는 시간 = 가장 오래 걸리는 시간을 측정해야하는데 최댓값을 뽑아야 하나?
time = 0
for i in distance:
if i != INF:
count += 1
time = max(time, i)

print(count - 1, time) # 시작노드 제외해야 해서 -1 해줘야 하는데 안함