diff --git "a/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/test.py" "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/test.py" new file mode 100644 index 0000000..f37165d --- /dev/null +++ "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/test.py" @@ -0,0 +1,2 @@ +daily = [0 for _ in range(26)] +print(daily) \ No newline at end of file diff --git "a/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" new file mode 100644 index 0000000..c782001 --- /dev/null +++ "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" @@ -0,0 +1,29 @@ +def solution(student, k): + answer = 0 + + size = len(student) + a, b = 0, 1 + + while a < size and b < size+1: + num = 0 # 재학생 수 + flag = 0 + left = 0 + for i in range(a, b): + if student[i]: + num += 1 + flag = 1 + if not flag: + left += 1 + + if num == k: + answer += (left + 1) + b += 1 + + + elif num < k: + b += 1 + elif num > k: + a += 1 + + return answer + diff --git "a/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" new file mode 100644 index 0000000..c436bf7 --- /dev/null +++ "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" @@ -0,0 +1,48 @@ +''' +매일 k번이상 +총합 2nk이상 +''' + +def solution(research, n, k): + answer = '' + arr = [[0]*26 for _ in range(len(research))] + daily = [0 for _ in range(26)] + total = [0 for _ in range(26)] + succesion = [0 for _ in range(26)] + + for i in range(len(research)): + for j in research[i]: + arr[i][ord(j)-97] += 1 + + # for i in range(len(research)): + # print(arr[i]) + + for i in range(26): + + for a in range(len(research)-n+1): + day = 0 + temp = 0 + for b in range(a, a+n): + if arr[b][i] >= k: + day += 1 + temp += arr[b][i] + else: + day = 0 + temp = 0 + + if day >= n and temp >= 2*n*k: #이슈 검색어 + succesion[i] += 1 + #print(succesion) + result = -1 + for i in range(26): + if succesion[i] == max(succesion) and max(succesion)>0: + result = i + break + #print(result) + if result > -1: + answer += chr(result+97) + else: + answer += 'None' + + return answer + diff --git "a/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" new file mode 100644 index 0000000..f637f72 --- /dev/null +++ "b/cmkim/2021 \353\235\274\354\235\270\352\263\265\354\261\204/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" @@ -0,0 +1,49 @@ +# 무적권 재귀문제, 소인수분해 +def factorization(x): + d = 2 + result = [] + while d <= x: + if x % d == 0: + result.append(d) + x = x / d + else: + d = d + 1 + return result + + +def recur(arr, divide): + result = [] + length = len(arr) # 부분배열의 갯수 + + + + print('arr = ', arr) + #print('length = ', length) + for i in range(length): + node = [[] for _ in range(divide)] + for j in range(len(arr[i])): + index = j % divide + node[index].append(arr[i][j]) + + for k in range(divide): + result.append(node[k]) + + + print('result = ', result) + return result + + +def solution(n): + answer = [] + p = [] # 소인수분해 결과 + p = factorization(n) + arr = list(range(1, n + 1)) + for i in range(len(p)): + if i == 0: + arr = recur([arr], p[i]) + else: + arr = recur(arr, p[i]) + for i in range(n): + answer.append(arr[i][0]) + + return answer \ No newline at end of file diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" new file mode 100644 index 0000000..39d9b7b --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2151.py" @@ -0,0 +1,38 @@ +def solution(id_list, report, k): + answer = [] + size = len(id_list) + arr = [[0] * size for _ in range(size)] + result = [] + dic = {string: i for i, string in enumerate(id_list)} + #print(dic) + + for i in range(len(report)): + str = report[i].split() + a, b = str[0], str[1] + # print(a, b) + arr[dic[a]][dic[b]] += 1 # a가 b를 신고한것 + + # for i in range(size): + # print(arr[i]) + + for i in range(size): + sum = 0 + for j in range(size): + if arr[j][i]: + sum += 1 + + if sum >= k: + result.append(1) + else: + result.append(0) + + # print('result = ',result) + + for i in range(size): + sum = 0 + for j in range(size): + if arr[i][j] and result[j]: + sum += 1 + answer.append(sum) + + return answer diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" new file mode 100644 index 0000000..4fc7a1c --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2152.py" @@ -0,0 +1,72 @@ +import string +import math +tmp = string.digits + + +def convert(num, base): + if base == 10: + return num + q, r = divmod(num, base) + if q == 0: + return tmp[r] + else: + return convert(q, base) + tmp[r] + + +def is_prime(x): + if x == 1: + return False + for i in range(2, int(math.sqrt(x))+1): + if x % i == 0: + return False + + return True + + +def compress(x): + result = '' + flag = 0 + right = 0 + # print(type(x)) + for i in range(len(x) - 1, -1, -1): + # print('i = ',i) + if x[i] == '0': + right += 1 + else: + break + # print('right = ', right) + if right: + x = x[0:-(right)] + # print('x = ',x) + for i in range(len(x)): + if x[i] != '0': + result += x[i] + flag = 0 + elif flag == 0: + result += '0' + flag = 1 + elif flag == 1: + continue + # print(result) + return result + + +def solution(n, k): + answer = -1 + num = convert(n, k) + # print(num) + arr = compress(str(num)) # 0압축하기, 오른쪽 0 제거 + # print(arr) + # print(type(arr)) + arr = arr.split('0') # 0제거한 수 + prime = [] + print(arr) + for i in range(len(arr)): # 0제거한 수에서 소수뽑아내기 + # print(int(arr[i])) + if is_prime(int(arr[i])): + prime.append(int(arr[i])) + # print(prime) + + answer = len(prime) + + return answer diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2153.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2153.py" new file mode 100644 index 0000000..b0b5df3 --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2153.py" @@ -0,0 +1,58 @@ +import math +def solution(fees, records): + answer = [] + + arr = [['']*3 for _ in range(len(records))] + cars = '' + for i in range(len(records)): + t, n, c = records[i].split() #시간, 차 번호, 상태 + arr[i][0] += t + arr[i][1] += n + arr[i][2] += c + if n not in cars: + cars += n + cars += ' ' + + # for i in range(len(records)): + # print(arr[i]) + + # print(cars) + car = cars.split() + #print(car) + car.sort()# 자동차번호 + #print(car) + time = [0 for _ in range(len(car))] # 이용시간 저장 + money = [0 for _ in range(len(car))] + for n in range(len(car)):#번호가 작은 차부터 + flag = 0 # 0=들어오기전, 1= 들어온상태 + s_time, e_time = 0, 0 #입차, 출차시간 + for i in range(len(records)): + if car[n] == arr[i][1]: + if flag == 1: + h, m = arr[i][0].split(':') + e_time = int(h) * 60 + int(m) + time[n] += e_time - s_time + flag = 0 + continue + if flag == 0: + h, m = arr[i][0].split(':') + s_time = int(h)*60 + int(m) + #print(h, m) + flag = 1 + if flag == 1: + e_time = 1439 + time[n] += e_time - s_time + + if time[n] >= fees[0]: + money[n] += fees[1] + time[n] -= fees[0] + money[n] += int(math.ceil(time[n] / fees[2]) *fees[3]) + else: + money[n] += fees[1] + + print(money) + + + return money + +#solution([180, 5000, 10, 600], ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"]) \ No newline at end of file diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" new file mode 100644 index 0000000..ec6ff78 --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2154.py" @@ -0,0 +1,3 @@ +def solution(n, info): + answer = [] + return answer \ No newline at end of file diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155.py" new file mode 100644 index 0000000..60de082 --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155.py" @@ -0,0 +1,69 @@ +# dp랑 플로이드 워셜? +# INF = 1e9 +def floyd(arr, dp1, dp2, info): + + size = len(info) + v1 = [[0] * size for _ in range(size)] + v2 = [[0] * size for _ in range(size)] + for i in range(size): + for j in range(size): + if arr[i][j]: #j가 자식노드고 + if info[j]: #j가 늑대면 + dp2[i][j] += 1 + v2[i][j] = 1 + # if dp1[i][j] <= dp2[i][j]: + # dp1[i][j] = 0 + else: + dp1[i][j] += 1 + v1[i][j] = 1 + for k in range(size): + for i in range(size): + for j in range(size): + if arr[i][k] and arr[k][j] and not v1[i][k] and not v2[k][j]: + dp1[i][j] = dp1[i][k] + dp1[k][j] + dp1[i][j] + dp2[i][j] = dp2[i][k] + dp2[k][j] + dp2[i][j] + for i in range(size): + print(dp1[i]) + print('=====================') + for i in range(size): + print(dp2[i]) + print('=====================') + + dp3 = [[0] * size for _ in range(size)] + for i in range(size): + for j in range(size): + dp3[i][j] = dp1[i][j] - dp2[i][i] + for i in range(size): + print(dp3[i]) + sum = 0 + for i in range(size): + for j in range(size): + if dp3[i][j] > 0: + sum += dp3[i][j] + return sum +def solution(info, edges): + answer = 0 + size = len(info) + # print(type(size)) + arr = [[0] * size for _ in range(size)] + dp1 = [[0] * size for _ in range(size)] #양, 늑대 + dp2 = [[0] * size for _ in range(size)] + #dp1[0][0] = 1 + #print(dp) + + + # for i in range(size): + # arr[i][i] = INF + # print(arr[i]) + for i in range(len(edges)): + a, b = edges[i][0], edges[i][1] + # print('a, b = ',a, b) + arr[a][b] = 1 # a의 자식은 b다 + arr[b][a] = 1 + # for i in range(size): + # print(arr[i]) + result = floyd(arr, dp1, dp2, info) + + return result + +#solution([0,0,1,1,1,0,1,0,1,0,1,1], [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]]) \ No newline at end of file diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155_2.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155_2.py" new file mode 100644 index 0000000..1c84e0a --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2155_2.py" @@ -0,0 +1,70 @@ +from collections import deque + + +def find_parent(parent, x): + if parent[x] != x: + parent[x] = find_parent(parent, parent[x]) + return parent[x] + + +def union_parent(parent, a, b): + a = find_parent(parent, a) + b = find_parent(parent, b) + if a < b: + parent[b] = a + else: + parent[a] = b + + +def bfs(line, v, visited): + queue = deque([v]) + visited[v] = 1 + + while (queue): + v = queue.popleft() # 시작 기준이 되는 값을 잡고 시작한다. + + for i in range(len(line)): # 기준값 주변에 있는 방문하지 않은 값들을 전부 탐색한다. + if line[i][0] == v: + for j in range(len(line)): + if line[j][0] == line[i][1]: + e = line[j][1] + line.append((v, e, line[i][2]+line[j][2])) + # if line[i][2]+line[j][2] > 0: + # queue.append(e) + + for i in range(len(line)): + print(line[i]) + +def solution(info, edges): + line = [] + answer = 0 + v = len(info) + e = len(edges) + parent = [0] * (v + 1) + + for i in range(1, v + 1): + parent[i] = i + + for i in range(e): + a, b = edges[i][0], edges[i][1] + if a == 0: + cost = 1.01 + else: + cost = 0 + if info[b]: # 늑대 + cost += -1 + else: + cost += 1.01 + line.append((a, b, cost)) + for i in range(len(edges)): + print(line[i]) + + # for edge in line: + # cost, a, b = edge + # + # if find_parent(parent, a) != find_parent(parent, b): + # union_parent(parent, a, b) + # result += cost + + visited = [0] * len(info) + bfs(line, 0, visited) diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2156.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2156.py" new file mode 100644 index 0000000..c0db6d5 --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2156.py" @@ -0,0 +1,23 @@ +def solution(board, skill): + answer = 0 + r = len(board) + c = len(board[0]) + + for i in range(len(skill)): + type, r1, c1, r2, c2, num = skill[i] + #print(type, r1, c1, r2, c2, num) + if type == 1: + for i in range(r1, r2+1): + for j in range(c1, c2+1): + board[i][j] -= num + else: + for i in range(r1, r2+1): + for j in range(c1, c2+1): + board[i][j] += num + + for i in range(r): + for j in range(c): + if board[i][j]>0: + answer += 1 + + return answer \ No newline at end of file diff --git "a/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2157.py" "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2157.py" new file mode 100644 index 0000000..1c73bc0 --- /dev/null +++ "b/cmkim/2021 \354\271\264\354\271\264\354\230\244 BLIND \354\230\210\354\204\240/\355\224\204\353\241\234\352\267\270\353\236\230\353\260\2157.py" @@ -0,0 +1,3 @@ +def solution(board, aloc, bloc): + answer = -1 + return answer \ No newline at end of file diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_2533.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_2533.py" index c3de7f9..1132d2a 100644 --- "a/cmkim/BaekJoon/\353\260\261\354\244\200_2533.py" +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_2533.py" @@ -1,5 +1,6 @@ import sys input = sys.stdin.readline + sys.setrecursionlimit(10**9) n = int(input()) tree = [[] for _ in range(n+1)] diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_2931.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_2931.py" index de95a35..ae84733 100644 --- "a/cmkim/BaekJoon/\353\260\261\354\244\200_2931.py" +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_2931.py" @@ -3,27 +3,127 @@ dx = [-1, 1, 0, 0] # 상하좌우 dy = [0, 0, -1, 1] -way = {'M': [0, 1, 2, 3], '|': [0, 1], '-': [2, 3], '+': [0, 1, 2, 3], '1': [1, 3], '2': [0, 3], '3': [0, 2], '4': [1, 2]} + +way = {'|': [0, 1], '-': [2, 3], '+': [0, 1, 2, 3], '1': [1, 3], '2': [0, 3], '3': [0, 2], + '4': [1, 2]} + + def bfs(x, y): - dir = way[arr[x][y]] - for i in dir: + global ax, ay #정답 파이프의 위치를 저장할 변수 + + q = deque() + q.append((x, y)) + while q: + x, y = q.popleft() + visited[x][y] = 1 + dir = way[arr[x][y]] #파이프의 모양에 따라 상하좌우중 어디로 갈지 정한다. + + for i in dir: + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < r and 0 <= ny < c and not visited[nx][ny]: + #print(arr[nx][ny]) + if arr[nx][ny] != '.': # 파이프가 있는 곳이라면 방문해서 진행한다. + if arr[nx][ny] != 'Z': + visited[nx][ny] = 1 + q.append((nx, ny)) + else: # 진행방향대로 진행했는데 파이프가 없다면 정답파이프의 위치이다. + ax, ay = nx, ny + #print('ax, ay = ', ax, ay) + for j in range(4): + cx = nx + dx[j] + cy = ny + dy[j] + if 0 <= cx < r and 0 <= cy < c and arr[cx][cy] != '.' and arr[cx][cy] != 'M' and arr[cx][cy] != 'Z': # 연결된 통로 구하기 + if can_move(cx, cy, nx, ny): + check.append(j) + return + +def firstblock(x, y): # 첫번째 파이프를 찾는 함수 + for i in range(4): nx = x + dx[i] ny = y + dy[i] + if 0 <= nx < r and 0 <= ny < c and arr[nx][ny] != '.': + visited[x][y] = 1 + return nx, ny - +def can_move(sx, sy, ex, ey): #연결된 파이프를 찾기위해 sx, sy -> ex, ey 이동이 가능한지 찾는 함수 + dir = way[arr[sx][sy]] + flag = False + for i in dir: + nx = sx + dx[i] + ny = sy + dy[i] + if 0 <= nx < r and 0 <= ny < c and nx == ex and ny == ey: + flag = True + return flag r, c = map(int, input().split()) -arr = [] -for i in range(r): - arr.append(list(input())) +visited = [[0]*c for _ in range(r)] +check = [] #지워진 공간에서 어디로 이어질지 저장 +#arr = [] +# for i in range(r): +# arr.append(list(input())) +arr = [list(input()) for _ in range(r)] + +# for i in range(r): +# print(arr[i]) + for i in range(r): for j in range(c): if arr[i][j] == 'M': x, y = i, j -# for i in range(r): -# print(arr[i]) + visited[x][y] = 1 + + +fx, fy = firstblock(x, y) +#print('fx, fy = ', fx, fy) +bfs(fx, fy) + + +#print(check) # 0 1 2 3 = 상 하 좌 우 + +if check == [0, 1]: + result = '|' +elif check == [2, 3]: + result = '-' +elif check == [0, 1, 2, 3]: + result = '+' +elif check == [1, 3]: + result = '1' +elif check == [0, 3]: + result = '2' +elif check == [0, 2]: + result = '3' +elif check == [1, 2]: + result = '4' + +print(ax+1, ay+1, result) + +''' +3 7 +.14.... +.M.Z... +..23... + +3 5 +..1-M +.Z+4. +..2.. + +3 7 +....... +.M-.Z.. +....... + +2 4 +M1-Z +2-.. + +2 3 +M.Z +2.3 + +''' -bfs(x, y) diff --git "a/cmkim/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" "b/cmkim/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" new file mode 100644 index 0000000..f5644a0 --- /dev/null +++ "b/cmkim/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274.py" @@ -0,0 +1,66 @@ +import heapq +import sys +input = sys.stdin.readline +INF = int(1e9) # 무한을 의미하는 값으로 10억을 설정 + +# 노드의 개수, 간선의 개수를 입력받기 +n, m = map(int, input().split()) +# 시작 노드 번호를 입력받기 +start = int(input()) +# 각 노드에 연결되어 있는 노드에 대한 정보를 담는 리스트를 만들기 +graph = [[] for i in range(n + 1)] +# 최단 거리 테이블을 모두 무한으로 초기화 +distance = [INF] * (n + 1) + +# 모든 간선 정보를 입력받기 +for _ in range(m): + a, b, c = map(int, input().split()) + # a번 노드에서 b번 노드로 가는 비용이 c라는 의미 + graph[a].append((b, c)) + +def dijkstra(start): + q = [] + # 시작 노드로 가기 위한 최단 경로는 0으로 설정하여, 큐에 삽입 + heapq.heappush(q, (0, start)) + distance[start] = 0 + while q: # 큐가 비어있지 않다면 + # 가장 최단 거리가 짧은 노드에 대한 정보 꺼내기 + dist, now = heapq.heappop(q) + # 현재 노드가 이미 처리된 적이 있는 노드라면 무시 + if distance[now] < dist: + continue + # 현재 노드와 연결된 다른 인접한 노드들을 확인 + for i in graph[now]: + cost = dist + i[1] + # 현재 노드를 거쳐서, 다른 노드로 이동하는 거리가 더 짧은 경우 + if cost < distance[i[0]]: + distance[i[0]] = cost + heapq.heappush(q, (cost, i[0])) + +# 다익스트라 알고리즘을 수행 +dijkstra(start) + +# 모든 노드로 가기 위한 최단 거리를 출력 +for i in range(1, n + 1): + # 도달할 수 없는 경우, 무한(INFINITY)이라고 출력 + if distance[i] == INF: + print("INFINITY") + # 도달할 수 있는 경우 거리를 출력 + else: + print(distance[i]) + +''' +6 11 +1 +1 2 2 +1 3 5 +1 4 1 +2 3 3 +2 4 2 +3 2 3 +3 6 5 +4 3 3 +4 5 1 +5 3 1 +5 6 2 +''' \ No newline at end of file