Skip to content

Commit

Permalink
2024-06-03
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jun 5, 2024
1 parent 5aee288 commit c535de3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
| 56์ฐจ์‹œ | 2023.05.18 | BFS | <a href="https://www.acmicpc.net/problem/17836">๊ณต์ฃผ๋‹˜์„ ๊ตฌํ•ด๋ผ!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193
| 57์ฐจ์‹œ | 2023.05.26 | ๋ถ„ํ•  ์ •๋ณต | <a href="https://www.acmicpc.net/problem/2263">ํŠธ๋ฆฌ์˜ ์ˆœํšŒ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/197
| 58์ฐจ์‹œ | 2023.05.30 | ๋ฐฑํŠธ๋ž˜ํ‚น | <a href="https://www.acmicpc.net/problem/1799">๋น„์ˆ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/204
| 59์ฐจ์‹œ | 2023.06.03 | ๋‹ค์ต์ŠคํŠธ๋ผ + ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/14942">๊ฐœ๋ฏธ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/207
---
Original file line number Diff line number Diff line change
@@ -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<right:
mid = (left+right)//2
#print(left, mid, right)

if check(mid, now_road, now_energy):
temp = mid
left = mid

else: right = mid

answer[now_idx] = now_road[temp][0]
#print(answer[1:])
#print()
now_road.popleft()

for ans in answer[1:]: print(ans)

0 comments on commit c535de3

Please sign in to comment.