From 841fc12cc52b10b44987e994fe731a0ce06fd1aa Mon Sep 17 00:00:00 2001 From: Youjiiin Date: Tue, 23 Jan 2024 01:15:29 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=84=EB=B3=B4=5F=EC=9C=A0=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65\354\212\244\355\212\270\353\235\274.py" | 2 + .../\354\234\240\354\247\204.py" | 28 ++++++++ .../\354\234\240\354\247\204.py" | 69 ++++++++++++------- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" index 31b07f4..7fac2d6 100644 --- "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" +++ "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" @@ -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) # 이미 처리된 적 있는가? diff --git "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230_\353\217\204\354\213\234/\354\234\240\354\247\204.py" "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230_\353\217\204\354\213\234/\354\234\240\354\247\204.py" index e69de29..cf5741a 100644 --- "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230_\353\217\204\354\213\234/\354\234\240\354\247\204.py" +++ "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230_\353\217\204\354\213\234/\354\234\240\354\247\204.py" @@ -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) \ No newline at end of file diff --git "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264/\354\234\240\354\247\204.py" "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264/\354\234\240\354\247\204.py" index cf5741a..448a899 100644 --- "a/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264/\354\234\240\354\247\204.py" +++ "b/\354\235\264\354\275\224\355\205\214/09 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264/\354\234\240\354\247\204.py" @@ -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) \ No newline at end of file + 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 해줘야 하는데 안함 \ No newline at end of file