Operating a Travel Agency
Time limit4sMemory limit1024 MB
Given a weighted tree where each city has a bus range, find for each city the spread of enjoyment values among all cities reachable from it by chaining buses.
- Level
Medium7 of 10
- Topics
- Tree, Graph, Shortest path, Divide and conquer
- Solved
- No attempts yet
Problem
You are the CEO of a successful travel agency. You want to expand the agency into a new country, and to do so you must choose the first base city.
The country has cities and two-way roads between them. Starting from any city, you can reach every other city by following the roads.
Each city has a value that represents how enjoyable the city is. The larger the value, the more enjoyable the city.
The country has an unusual restriction. The tour buses that leave city can only be boarded in city , and they can only travel to cities within distance of city .
A tour bus from city can be boarded at any time as long as you are in city . So you can board passengers at the base city, travel to another city, and then board that city's tour bus to go farther. Switching buses many times lets you reach far more cities than using only the buses from the base city.
When choosing a base city, what matters is the difference between the maximum and minimum enjoyment values among the cities reachable from it.
Given the cities and roads, find for every city, when it is chosen as the base city, the difference between the maximum and minimum enjoyment values of the cities reachable from it.
Input
The first line contains the number of cities . ()
The second line contains the enjoyment values of the cities, integers separated by spaces. ()
The third line contains the distances , integers separated by spaces, where is the distance the tour bus leaving city can travel. ()
From the fourth line onward, there are lines. Each line contains , , and separated by spaces, meaning a road of length connects cities and . (, , )
All given numbers are integers.
Output
For each city , print on the -th line the difference between the maximum and minimum enjoyment values of the cities reachable from city when it is the base city. Print lines in total.