Taxi

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

요약
가중치가 있는 트리에서 각 도시마다 요금이 a_v + b_v * 거리인 택시를 갈아타며 도시 1에서 다른 모든 도시로 가는 최소 비용을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 동적 계획법, DFS, 최단 경로
정답자
아직 제출이 없습니다

문제

There are nn cities that are connected by n−1n-1 roads, forming a tree. Note that each road has a given length.

When you are at city vv, you can take a taxi of the local taxi company to any other city ww. For this, you have to pay a_v+d⋅b_va\_v + d \cdot b\_v cookies, where dd is the distance from vv to ww. In other words, you have to pay the base cost a_va\_v and additionally b_vb\_v for each unit of distance traveled.

You are currently at city 11, and for each other city vv, you want to know the minimum cost to get there.

입력

The first line contains one integer nn (2≤n≤1052 \leq n \leq 10^5) --- the number of cities.

The second line contains nn integers a_ia\_i (0≤a_i≤10120 \leq a\_i \leq 10^{12}) --- the base costs of the taxis.

The third line contains nn integers b_ib\_i (1≤b_i≤1061 \leq b\_i \leq 10^6) --- the cost per distance.

Then n−1n-1 lines follow, describing the roads between the cities. Every line contains three integers u,v,u, v, and ℓ\ell (1≤u,v≤n1 \leq u, v \leq n, u≠vu \neq v, 1≤ℓ≤1061 \leq \ell \leq 10^6) describing a bidirectional road between cities uu and vv of length ℓ\ell.

출력

Output a single line containing n−1n-1 integers. The ii-th of them should be the minimum cost to get to city i+1i+1.

힌트

Consider the cost to get to city 33 in the first sample: Driving directly from 11 to 33 would cost 0+7⋅8=560 + 7 \cdot 8 = 56. It is better to drive from 11 to 22 with a cost of 88 and take a second taxi from 22 to 33 with a cost of 1+8⋅4=331 + 8 \cdot 4 = 33. While the distance traveled is larger, the cost is still smaller.

예제2

  1. 예제 1

    입력
    3
    0 1 2
    8 4 4
    1 2 1
    1 3 7
    
    예상 출력
    8 41
    
  2. 예제 2

    입력
    2
    353 313
    928248 475634
    2 1 898027
    
    예상 출력
    833591767049