Under construction

Still being built. Things may change

Taxi Trip

Time limit2sMemory limit1024 MB

Summary
On a weighted tree, find the minimum taxi fare from city 0 to every city, where each city's taxi has its own base fare and per-km rate.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, DFS
Solved
No attempts yet

Problem

The IOI country has NN cities and N−1N - 1 bidirectional roads connecting them, and any two different cities can be reached from each other using only roads. In other words, the road network of the IOI country forms a tree.

Each city has a distinct number from 00 to N−1N - 1, and city 00 is the capital of the IOI country. For every 0≤i≤N−20 \le i \le N - 2, road ii connects city U[i]U[i] and city V[i]V[i], and its length is W[i]W[i] km.

Taxi fares differ by city. For every 0≤i≤N−10 \le i \le N - 1, a taxi departing from city ii has a base fare of A[i]A[i] won and a per-kilometer fare of B[i]B[i] won. If you ride a taxi from city ii for dd km, you pay A[i]+d×B[i]A[i] + d \times B[i] won.

Seohyun lives in city 0, the capital. She wants to travel to other cities by taxi. When she arrives at a city, she can keep riding her current taxi or switch to a taxi departing from that city. Switching taxis requires paying the base fare again, and the per-kilometer fare may change. For each city, compute the minimum cost to travel from city 0 to that city.

Constraints

  • 2≤N≤100 0002 \le N \le 100\,000
  • For all ii, 0≤A[i]≤10120 \le A[i] \le 10^{12} (0≤i≤N−10 \le i \le N - 1)
  • For all ii, 0≤B[i]≤1 000 0000 \le B[i] \le 1\,000\,000 (0≤i≤N−10 \le i \le N - 1)
  • For all ii, 0≤U[i],V[i]≤N−10 \le U[i], V[i] \le N - 1; U[i]≠V[i]U[i] \ne V[i] (0≤i≤N−20 \le i \le N - 2)
  • For all ii, 1≤W[i]≤1 000 0001 \le W[i] \le 1\,000\,000 (0≤i≤N−20 \le i \le N - 2)

Examples1

  1. Example 1

    Input
    2
    0 0
    0 0
    0 1 1
    
    Expected output
    0