본문 바로가기

나동빈 with 파이썬/실전문제 풀이21

[Ch.10 - 그래프 이론] 커리큘럼 My code from collections import deque import copy v = int(input()) indegree = [0] * (v + 1) graph = [[] for i in range(v + 1)] time = [0] * (v + 1) for i in range(1, v + 1): data = list(map(int,input().split())) time[i] = data[0] for x in data[1:-1]: graph[x].append(i) indegree[i] += 1 def topology_sort(): q = deque() result = copy.deepcopy(time) for i in range(1, v + 1): if indegree[i] == 0: q... 2022. 2. 21.
[Ch.10 - 그래프 이론] 도시 분할 계획 My code 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 n, m = map(int,input().split()) parent = [0] * (n + 1) edges = [] for i in range(1, n + 1): parent[i] = i for i in range(m): a, b, cost = map(int,input().. 2022. 2. 20.
[Ch. 10 - 그래프 이론] 팀 결성 My code 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 n, m = map(int,input().split()) parent = [0] * (n + 1) for i in range(1, n + 1): parent[i] = i for i in range(m): a, b, c = map(int, input().split()) if .. 2022. 2. 20.
[Ch. 9 - 최단 경로] 전보 My code import heapq INF = int(1e9) n, m, c = map(int,input().split()) graph = [[] for i in range(n + 1)] distance = [INF] * (n + 1) for i in range(m): x, y, z = map(int,input().split()) graph[x].append((y, z)) def dijkstra(c): q = [] distance[c] = 0 heapq.heappush(q, (0, c)) while q: dist, now = heapq.heappop(q) if distance[now] < dist: continue for i in graph[now]: cost = dist + i[1] if cost <.. 2022. 2. 17.