Cup of Tea

시간 제한1초메모리 제한2048 MB

요약
각 도로에 통행료가 있고 일부 도시의 찻집에서 행복도가 k만큼 오르는 나무에서, 행복도가 한 번도 음수가 되지 않도록 다른 모든 도시에 도달하는 최소 통행료 합을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 동적 계획법, 그리디, 이분 탐색
정답자
아직 제출이 없습니다

문제

Abolf lives in Aboland, a country consisting of nn cities and n−1n - 1 two-way roads. In Aboland, one can travel from any city to any other city using these roads. Aboland’s cities are numbered from 11 to nn.

Abolbucks is a multinational chain of teahouses which serves the best tea in the world. When Abolf enters a city with an Abolbucks branch, he drinks a cup of tea and instantly reaches kk units of happiness. However, each time Abolf travels through the iith road, he must pay c_ic\_i coins as toll which causes him to lose c_ic\_i units of happiness.

Abolf currently resides in city 11 and wants to plan his summer trip. If at any point during his trip Abolf’s happiness drops below zero, he would stops his trip immediately. For each city tt (for 2≤t≤n2 \le t \le n), Abolf wants to know what is the minimum amount of coins he should pay to reach city tt while making sure that his happiness remains non-negative at all time, including at the destination.

He has asked you to find this amount for each city except for his home city. Note that each destination should be considered separately. Also, he may visit a city multiple times during his trip.

입력

The first line of input contains two integers nn and kk (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5, 1≤k≤1091 \le k \le 10^9), the number of cities in Aboland and Abolf’s happiness after he drinks a cup of tea, respectively. Each of the next n−1n - 1 lines contains three space-separated integers v_iv\_i, u_iu\_i, and c_ic\_i (1≤v_i,u_i≤n1 \le v\_i , u\_i \le n, 1≤c_i≤1091 \le c\_i \le 10^9, u_i≠v_iu\_i \ne v\_i) indicating that the iith road connects city u_iu\_i and city v_iv\_i, and Abolf should pay c_ic\_i coins each time he travels through this road. The last line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \dots , a\_n (0≤a_i≤10 \le a\_i \le 1). If a_i=1a\_i = 1, there is an Abolbucks branch in city ii. It is guaranteed that a_1=1a\_1 = 1.

출력

In the only line of the output, you should print n−1n-1 integers. The iith number should be the minimum amount of coins it takes for Abolf to reach city i+1i + 1 from city 11. If there is no way to reach city i+1i + 1 such that Abolf’s happiness remains non-negative at all time, print −1-1 for that city.

예제1

  1. 예제 1

    입력
    6 3
    1 2 4
    1 3 3
    1 4 2
    4 5 1
    4 6 2
    1 1 0 0 1 0
    
    예상 출력
    -1 3 2 3 6