From c8ea99c1662352cd4252075d45d7c50e812877ee Mon Sep 17 00:00:00 2001 From: heerucan Date: Sun, 13 Mar 2022 14:44:51 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=BD=94=EB=94=A9=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=B1=85]=209-1.=20=EB=AF=B8=EB=9E=98=EB=8F=84=EC=8B=9C=20(#7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\353\236\230\353\217\204\354\213\234.py" | 33 +++++++++++++++++++ .../\354\240\204\353\263\264.py" | 0 2 files changed, 33 insertions(+) create mode 100644 "CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230\353\217\204\354\213\234.py" create mode 100644 "CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264.py" diff --git "a/CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230\353\217\204\354\213\234.py" "b/CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230\353\217\204\354\213\234.py" new file mode 100644 index 0000000..ed2a9ea --- /dev/null +++ "b/CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\353\257\270\353\236\230\353\217\204\354\213\234.py" @@ -0,0 +1,33 @@ +n, m = map(int, input().split()) +INF = int(1e9) # 무한을 의미하는 10억 + +# 2차원 리스트를 만들고, 모든 값을 무한으로 초기화 +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 _ in range(m): + # A -> B로 가는 비용은 C라고 설정 + a, b = map(int, input().split()) + graph[a][b] = 1 + graph[b][a] = 1 + +# 점화식에 따라 플로이드 워셜 알고리즘을 수행 +for k in range(1, n+1): + for a in range(1, n+1): + for b in range(1, n+1): + graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]) + +x, k = map(int, input().split()) + +if graph[1][k] or graph[k][x] == INF: + print(-1) +else: + print(graph[1][k] + graph[k][x]) + + diff --git "a/CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264.py" "b/CodingTest/CH9 \354\265\234\353\213\250 \352\262\275\353\241\234/\354\240\204\353\263\264.py" new file mode 100644 index 0000000..e69de29