D - Cup of Tea


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Abolf lives in Aboland, a country consisting of nn cities and n1n - 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 ithi^{th} road, he must pay cic_i coins as toll which causes him to lose cic_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 2tn2 \leq t \leq 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, the number of cities in Aboland and Abolf’s happiness after he drinks a cup of tea, respectively. Each of the next n1n -1 lines contains three space-separated integers viv_i, uiu_i, and cic_i

indicating that the ith{i^th} road connects city uiu_i and city viv_i, and Abolf should pay cic_i coins each time he travels through this road. The last line contains nn integers a1,a2,,ana_1, a_2, \dots , a_n (0ai10 \leq a_i \leq 1). If ai=1a_i = 1, there is an Abolbucks branch in city ii. It is guaranteed that a1=1a_1 = 1.

1ui,vin,uivi1 \leq u_i, v_i \leq n, u_i \neq v_i 1ci1091 \leq c_i \leq 10^9 2n3×1052 \leq n \leq 3 \times 10^5 1k1091 \leq k \leq 10^9

خروجی🔗

In the only line of the output, you should print n1n - 1 integers. The ithi^{th} 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.

مثال🔗

ورودی نمونه ۱🔗

6 3
1 2 4
1 3 3
1 4 2
4 5 1
4 6 2
1 1 0 0 1 0
Plain text

خروجی نمونه ۱🔗

-1 3 2 3 6
Plain text
ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.