From 5c53701ac91129592de2358694d743103b7fb80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Mon, 1 Jul 2024 18:48:36 +0900 Subject: [PATCH 1/3] =?UTF-8?q?2024-05-28=20=EC=B5=9C=EC=86=8C=EB=B9=84?= =?UTF-8?q?=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhyun113/README.md | 3 +- .../9-suhyun113.py" | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 "suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" diff --git a/suhyun113/README.md b/suhyun113/README.md index ac4efaf..b038c16 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -9,4 +9,5 @@ | 5차시 | 2024.04.10 | 스택 | [크레인 인형 뽑기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) | | 6차시 | 2024.04.14 | 스택 | [컨트롤 제트](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) | | 7차시 | 2024.05.09 | 트리 | [원숭이 매달기](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) | -| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | \ No newline at end of file +| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | +| 8차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) | \ No newline at end of file diff --git "a/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" new file mode 100644 index 0000000..1778f1f --- /dev/null +++ "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" @@ -0,0 +1,36 @@ +# 1916 : 최소비용 구하기 + +import heapq + +INF = float('inf') # 최대값 정의 + +N = int(input()) # 도시의 개수(노드) +M = int(input()) # 버스의 개수(에지) + +graph = [[] for _ in range(N+1)] # 그래프 인접 리스트로 초기화(방문하지 않은 노드들) +distance = [INF] * (N+1) # 각 노드까지의 거리 무한대로 초기화 + +for _ in range(M): + x, y, cost = map(int, input().split()) # x -> y 도시로 가는 데 필요한 비용 cost + graph[x].append([y, cost]) # 그래프에 에지 추가 + +start, end = map(int, input().split()) # 출발, 도착 노드 입력 받기 + +# 다익스트라 알고리즘 +def Dijkstra(start): + q = [] # 우선순위 큐 생성 + heapq.heappush(q, [0, start]) # 출발할 도시 큐에 넣기([거리, 노드] 형태) + distance[start] = 0 # 시작 도시의 거리 0으로 초기화 + + while q: # 큐가 빌 때까지 반복 + weight, node = heapq.heappop(q) # 현재 노드까지의 거리, 현재 노드(큐에서 가장 작은 값) + if distance[node] < weight: # 현재 노드가 이미 처리된 노드인지 확인 + continue + for n, w in graph[node]: + cost = w + weight # 현재 노드를 통해 인접 노드까지 가는 새로운 거리 계산 + if distance[n] > cost: # 새로운 거리가 기존에 저장된 거리보다 짧은지 확인 + distance[n] = cost # 최단 거리 갱신 + heapq.heappush(q, [cost, n]) # 인접 노드를 우선순위 큐에 추가 + +Dijkstra(start) +print(distance[end]) \ No newline at end of file From 8921313a0ad35d7a00dea60acd0c7d3d5b1c89ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Mon, 1 Jul 2024 18:49:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Revert=20"2024-05-28=20=EC=B5=9C=EC=86=8C?= =?UTF-8?q?=EB=B9=84=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 5c53701ac91129592de2358694d743103b7fb80b. --- suhyun113/README.md | 3 +- .../9-suhyun113.py" | 36 ------------------- 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 "suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" diff --git a/suhyun113/README.md b/suhyun113/README.md index b038c16..ac4efaf 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -9,5 +9,4 @@ | 5차시 | 2024.04.10 | 스택 | [크레인 인형 뽑기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) | | 6차시 | 2024.04.14 | 스택 | [컨트롤 제트](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) | | 7차시 | 2024.05.09 | 트리 | [원숭이 매달기](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) | -| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | -| 8차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) | \ No newline at end of file +| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | \ No newline at end of file diff --git "a/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" deleted file mode 100644 index 1778f1f..0000000 --- "a/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" +++ /dev/null @@ -1,36 +0,0 @@ -# 1916 : 최소비용 구하기 - -import heapq - -INF = float('inf') # 최대값 정의 - -N = int(input()) # 도시의 개수(노드) -M = int(input()) # 버스의 개수(에지) - -graph = [[] for _ in range(N+1)] # 그래프 인접 리스트로 초기화(방문하지 않은 노드들) -distance = [INF] * (N+1) # 각 노드까지의 거리 무한대로 초기화 - -for _ in range(M): - x, y, cost = map(int, input().split()) # x -> y 도시로 가는 데 필요한 비용 cost - graph[x].append([y, cost]) # 그래프에 에지 추가 - -start, end = map(int, input().split()) # 출발, 도착 노드 입력 받기 - -# 다익스트라 알고리즘 -def Dijkstra(start): - q = [] # 우선순위 큐 생성 - heapq.heappush(q, [0, start]) # 출발할 도시 큐에 넣기([거리, 노드] 형태) - distance[start] = 0 # 시작 도시의 거리 0으로 초기화 - - while q: # 큐가 빌 때까지 반복 - weight, node = heapq.heappop(q) # 현재 노드까지의 거리, 현재 노드(큐에서 가장 작은 값) - if distance[node] < weight: # 현재 노드가 이미 처리된 노드인지 확인 - continue - for n, w in graph[node]: - cost = w + weight # 현재 노드를 통해 인접 노드까지 가는 새로운 거리 계산 - if distance[n] > cost: # 새로운 거리가 기존에 저장된 거리보다 짧은지 확인 - distance[n] = cost # 최단 거리 갱신 - heapq.heappush(q, [cost, n]) # 인접 노드를 우선순위 큐에 추가 - -Dijkstra(start) -print(distance[end]) \ No newline at end of file From 7136243572b506d6899eccc47178246ae93bf9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Mon, 1 Jul 2024 18:51:01 +0900 Subject: [PATCH 3/3] =?UTF-8?q?2024-05-28=20=EC=B5=9C=EC=86=8C=EB=B9=84?= =?UTF-8?q?=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhyun113/README.md | 3 +- .../9-suhyun113.py" | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 "suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" diff --git a/suhyun113/README.md b/suhyun113/README.md index ac4efaf..163899f 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -9,4 +9,5 @@ | 5차시 | 2024.04.10 | 스택 | [크레인 인형 뽑기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) | | 6차시 | 2024.04.14 | 스택 | [컨트롤 제트](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) | | 7차시 | 2024.05.09 | 트리 | [원숭이 매달기](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) | -| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | \ No newline at end of file +| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | +| 9차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) | \ No newline at end of file diff --git "a/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" new file mode 100644 index 0000000..1778f1f --- /dev/null +++ "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" @@ -0,0 +1,36 @@ +# 1916 : 최소비용 구하기 + +import heapq + +INF = float('inf') # 최대값 정의 + +N = int(input()) # 도시의 개수(노드) +M = int(input()) # 버스의 개수(에지) + +graph = [[] for _ in range(N+1)] # 그래프 인접 리스트로 초기화(방문하지 않은 노드들) +distance = [INF] * (N+1) # 각 노드까지의 거리 무한대로 초기화 + +for _ in range(M): + x, y, cost = map(int, input().split()) # x -> y 도시로 가는 데 필요한 비용 cost + graph[x].append([y, cost]) # 그래프에 에지 추가 + +start, end = map(int, input().split()) # 출발, 도착 노드 입력 받기 + +# 다익스트라 알고리즘 +def Dijkstra(start): + q = [] # 우선순위 큐 생성 + heapq.heappush(q, [0, start]) # 출발할 도시 큐에 넣기([거리, 노드] 형태) + distance[start] = 0 # 시작 도시의 거리 0으로 초기화 + + while q: # 큐가 빌 때까지 반복 + weight, node = heapq.heappop(q) # 현재 노드까지의 거리, 현재 노드(큐에서 가장 작은 값) + if distance[node] < weight: # 현재 노드가 이미 처리된 노드인지 확인 + continue + for n, w in graph[node]: + cost = w + weight # 현재 노드를 통해 인접 노드까지 가는 새로운 거리 계산 + if distance[n] > cost: # 새로운 거리가 기존에 저장된 거리보다 짧은지 확인 + distance[n] = cost # 최단 거리 갱신 + heapq.heappush(q, [cost, n]) # 인접 노드를 우선순위 큐에 추가 + +Dijkstra(start) +print(distance[end]) \ No newline at end of file