Skip to content

Commit

Permalink
2ì알고리즘
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyeongchoi committed Feb 2, 2021
1 parent 50c4110 commit 7ef9ba5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Empty file added argorithm/len_wire_cutting.py
Empty file.
27 changes: 27 additions & 0 deletions argorithm/len_wire_cutting_gw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
A = sorted([int(input()) for _ in range(n)])
print(A)


def binary_search(A, m):
start = 0
end = sum(A)//m
result = 0
while start <= end:
mid = (start+end)//2
if mid == 0:
start += 1
continue
s = sum([a//mid for a in A])
print(s)
if s >= m:
result = max(result, mid)
start = mid+1
elif s < m:
end = mid-1
return result


print(binary_search(A, m))
31 changes: 31 additions & 0 deletions argorithm/shortest_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import heapq
input = sys.stdin.readline

V, E = map(int, input().split())
K = int(input())
distance = [sys.maxsize]*(V+1)
arr = [list(map(int, input().split())) for _ in range(E)]
graph = [[] for _ in range(V+1)]
for start, end, cost in arr:
graph[start].append((cost, end))
heap = []


def dijkstra(s_node):
heapq.heappush(heap, (0, s_node))
distance[s_node] = 0
while heap:
h_cost, h_node = heapq.heappop(heap)
if h_cost > distance[h_node]:
continue
for c, e in graph[h_node]:
sum_cost = h_cost+c
if sum_cost < distance[e]:
distance[e] = sum_cost
heapq.heappush(heap, (sum_cost, e))


dijkstra(K)
for i in range(1, V+1):
print('INF' if distance[i] == sys.maxsize else distance[i])
44 changes: 44 additions & 0 deletions argorithm/shortest_path_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys
import heapq
input = sys.stdin.readline
INF = sys.maxsize
V, E = map(int, input().split())
#시작점 K
K = int(input())
#가중치 테이블 dp
dp = [INF]*(V+1)
heap = []
graph = [[] for _ in range(V + 1)]
def Dijkstra(start):
#가중치 테이블에서 시작 정점에 해당하는 가중치는 0으로 초기화
dp[start] = 0
heapq.heappush(heap,(0, start))

#힙에 원소가 없을 때 까지 반복.
while heap:
wei, now = heapq.heappop(heap)

#현재 테이블과 비교하여 불필요한(더 가중치가 큰) 튜플이면 무시.
if dp[now] < wei:
continue

for w, next_node in graph[now]:
#현재 정점 까지의 가중치 wei + 현재 정점에서 다음 정점(next_node)까지의 가중치 W
# = 다음 노드까지의 가중치(next_wei)
next_wei = w + wei
#다음 노드까지의 가중치(next_wei)가 현재 기록된 값 보다 작으면 조건 성립.
if next_wei < dp[next_node]:
#계산했던 next_wei를 가중치 테이블에 업데이트.
dp[next_node] = next_wei
#다음 점 까지의 가증치와 다음 점에 대한 정보를 튜플로 묶어 최소 힙에 삽입.
heapq.heappush(heap,(next_wei,next_node))

#초기화
for _ in range(E):
u, v, w = map(int, input().split())
#(가중치, 목적지 노드) 형태로 저장
graph[u].append((w, v))

Dijkstra(K)
for i in range(1,V+1):
print("INF" if dp[i] == INF else dp[i])

0 comments on commit 7ef9ba5

Please sign in to comment.