본문 바로가기
  • KEEP HUSTLE!

분류 전체보기73

[백준 1717] 집합의 표현 - Python(파이썬) import sys def find(x): if my_set[x] < 0: return x my_set[x] = find(my_set[x]) return my_set[x] def union(x, y): x, y = find(x), find(y) if x == y: return if my_set[x] < my_set[y]: my_set[x] += my_set[y] my_set[y] = x else: my_set[y] += my_set[x] my_set[x] = y n, m = map(int, sys.stdin.readline().split()) my_set = [-1] * (n + 1) for _ in range(m): key, a, b = map(int, sys.stdin.readline().split(.. 2021. 3. 11.
[백준 6549] 히스토그램에서 가장 큰 직사각형 - Python(파이썬) 스택을 활용함 import sys def solution(data): arr = data[1:] stack = [] my_max = 0 for idx, value in enumerate(arr): my_idx = idx while stack and stack[-1][0] >= value: h, my_idx = stack.pop() my_max = max(my_max, (idx - my_idx) * h) stack.append((value, my_idx)) for h, idx in stack: my_max = max(my_max, (n - idx) * h) print(my_max) while True: data = list(map(int, sys.stdin.readline().split())) n = da.. 2021. 3. 11.
[백준 11657] 타임머신 - Python(파이썬) import sys def solution(): check = [float('inf')] * (N + 1) check[1] = 0 for n in range(1, N+1): for i in range(1, N+1): if check[i] == float('inf'): continue for k, l in dist[i].items(): l += check[i] if check[k] > l: if n == N: return [-1] check[k] = l return check[2:] N, M = map(int, sys.stdin.readline().split()) dist = {i: {} for i in range(1, N+1)} for _ in range(M): a, b, c = map(int, sys... 2021. 3. 11.
[백준 9370] 미확인 도착지 - Python(파이썬) from heapq import heappush, heappop import sys def solution(x): check = [100000000] * (n+1) check[x] = 0 my_heap = [(0, x)] while my_heap: k, l = heappop(my_heap) for o, p in dist[l].items(): p += k if check[o] > p: check[o] = p heappush(my_heap, (p, o)) return check T = int(sys.stdin.readline().strip()) for tc in range(T): n, m, t = map(int, sys.stdin.readline().split()) s, g, h = map(int, sys... 2021. 3. 11.
[백준 1504] 특정한 최단 경로 - Python(파이썬) from heapq import heappush, heappop import sys def solution(s): check = [float('inf')] * (N + 1) check[s] = 0 temp = [(0, s)] while temp: x, y = heappop(temp) if check[y] >= x: for n, m in dist[y]: m += x if check[n] > m: check[n] = m heappush(temp, (m, n)) return check N, E = map(int, sys.stdin.readline().split()) dist = {i: [] for i in range(N + 1)} for e in range(E): a, b, c = map(int, sys.st.. 2021. 3. 11.
[백준 1753] 최단경로 - Python(파이썬) from heapq import heappush, heappop import sys def solution(): while my_heap: my_x, my_y = heappop(my_heap) if check[my_y] >= my_x: for k, l in dist[my_y].items(): l += my_x if check[k] > l: check[k] = l heappush(my_heap, [l, k]) V, E = map(int, sys.stdin.readline().split()) start = int(sys.stdin.readline().strip()) check = [float('inf')] * (V + 1) my_heap = [] dist = [{} for _ in range(V+1)] ad.. 2021. 3. 11.