달나라에 사는 토끼와 우주에서 떨어지는 떡
Time limit1sMemory limit1024 MB
Rabbits on a directed functional graph walk shortest paths to each falling rice cake's vertex, and the task is to find each rabbit's total jump count.
- Level
Hard8 of 10
- Topics
- Tree, Graph, Shortest path, DFS
- Solved
- No attempts yet
문제
There are rabbits living on the Moon. The Moon is a graph made of vertices and directed edges. Initially, rabbit is at vertex . Each vertex has exactly one vertex that it can reach directly without passing through another vertex. The edge from vertex to vertex has length .
The rabbits on the Moon love rice cakes. Normally a rabbit does not leave its vertex, but when a rice cake falls from space, every rabbit moves very eagerly. rice cakes fall in order. The -th rice cake falls on vertex . Every rabbit wants to move to the vertex where the cake falls, but edges have directions, so only rabbits that can reach vertex move there. A rabbit that cannot reach vertex stays where it is.
A rabbit always follows a shortest path so that it reaches the vertex as fast as possible. Even when two paths have the same length, the number of jumps needed can differ between rabbits. For rabbit , moving a distance of requires jumps. If rabbit moves along a path of length , it makes jumps.
The process repeats: a rice cake falls, the rabbits move, and each rabbit that reached the cake's vertex waits there until the next cake falls. The next cake falls only after the last rabbit to arrive has finished moving. When no cakes remain, the process ends.
Let be the total number of jumps rabbit makes from the moment the first rice cake falls until the last cake falls and all movement is finished. Find .
Input
The first line contains .
Each of the next lines contains and , separated by a space.
Line contains , separated by spaces.
Line contains .
Line contains , separated by spaces.
All numbers in the input are integers.
Output
Print on the -th line for from to .