Skip to content

Commit

Permalink
1월 25일 알고리즘
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyeongchoi committed Jan 25, 2021
1 parent c9c942e commit 3196566
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
15 changes: 15 additions & 0 deletions argorithm/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def solution(n):
arr = []
for i in range(n+1):
if i == 0:
arr.append(i)
elif i == 1:
arr.append(i)
else:
arr.append(arr[i-1]+arr[i-2])
return arr[n] % 1234567


if __name__ == '__main__':
real_answer = solution(5)
print(real_answer)
29 changes: 29 additions & 0 deletions argorithm/minimum_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
from heapq import heappush, heappop

input = sys.stdin.readline
N = int(input())
M = int(input())
graph = [[] for _ in range(N + 1)]
for i in range(M):
a, b, c = map(int, input().split())
graph[a].append((b, c))
s, e = map(int, input().split())


def dijkstra(start, end):
heap = []
heappush(heap, (0, start)) # 시작 지점 힙에 추가
distance = [sys.maxsize] * (N+1) # 각 정점사이의 거리 무한대로 초기화
distance[start] = 0 # 시작 지점 0으로 초기화
while heap:
weight, index = heappop(heap)
for e, c in graph[index]:
if distance[e] > weight + c:
distance[e] = weight + c
heappush(heap, (weight+c, e))
print(distance)
return distance[end]


print(dijkstra(s, e))
28 changes: 28 additions & 0 deletions argorithm/minimum_cost_practice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
import heapq

input = sys.stdin.readline
N = int(input())
M = int(input())
graph = [[] for _ in range(N+1)]
for i in range(M):
a, b, c = map(int, input().split())
graph[a].append((b, c))
s, e = map(int, input().split())


def dijkstra(start, end):
heap = []
heapq.heappush(heap, (0, start))
distance = [sys.maxsize] * (N+1)
distance[start] = 0
while heap:
weight, index = heapq.heappop(heap)
for n, w in graph[index]:
if distance[n] > weight + w:
distance[n] = weight + w
heapq.heappush(heap, (weight+w, n))
return distance[end]


print(dijkstra(s, e))

0 comments on commit 3196566

Please sign in to comment.