diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 093a7e46..e1268ba2 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -63,4 +63,5 @@ | 56차시 | 2023.05.18 | BFS | 공주님을 구해라! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193 | 57차시 | 2023.05.26 | 분할 정복 | 트리의 순회 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/197 | 58차시 | 2023.05.30 | 백트래킹 | 비숍 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/204 +| 59차시 | 2023.06.03 | 다익스트라 + 이분 탐색 | 개미 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/207 --- diff --git "a/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\352\260\234\353\257\270.py" "b/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\352\260\234\353\257\270.py" new file mode 100644 index 00000000..5319230e --- /dev/null +++ "b/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\352\260\234\353\257\270.py" @@ -0,0 +1,85 @@ +from collections import defaultdict, deque +from heapq import * +import sys + +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) +ant = [int(input()) for _ in range(N)] +graph_info = defaultdict(lambda : defaultdict(int)) +neighbor_count = [0 for _ in range(N+1)] + +for _ in range(N-1): + start, destination, cost = map(int, input().split()) + graph_info[start][destination] = cost + graph_info[destination][start] = cost + + neighbor_count[start] += 1 + neighbor_count[destination] += 1 + +cost = [int(1e9) for _ in range(N+1)] +heap = [(0,1)] +parent = [0 for idx in range(N+1)] + +while heap: + now_cost, now_node = heappop(heap) + + if now_cost >= cost[now_node] and now_node != 1: continue + cost[now_node] = now_cost + + for next_node, next_cost in graph_info[now_node].items(): + if now_cost + next_cost < cost[next_node]: + heappush(heap, (now_cost + next_cost, next_node)) + parent[next_node] = now_node + +leaf_node = deque() +for node, value in enumerate(neighbor_count): + if node in (0, 1): continue + if value == 1: leaf_node.append(node) + +#print(leaf_node) + +def check(mid, now_road, energy): + if now_road[0][1] - now_road[mid][1] <= energy: return True + return False + +answer = [0 for _ in range(N+1)] + +#print() +#print(ant) +for leaf in leaf_node: + now_node = leaf + now_road = deque() + while now_node != 0: + now_road.append((now_node, cost[now_node])) + now_node = parent[now_node] + + while now_road: + now_idx, now_distance = now_road[0] + now_energy = ant[now_idx-1] + + if answer[now_idx] != 0: + now_road.popleft() + continue + + # print(now_road) + + left = -1 + right = len(now_road) + temp = 0 + while left+1