Commute
Time limit1sMemory limit1024 MB
A connected undirected graph has K alternative weight vectors; Yun may switch weight vectors at most K times at vertices and wants the cheapest trip from A to B.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Heap
- Solved
- No attempts yet
Problem
Yun lives in Uni Village. Uni Village has buildings numbered through . Yun lives in building and commutes every day to the company in building .
Uni Village has the following structure. There are bidirectional roads connecting the buildings, and by using the roads in a suitable order one can travel between any two buildings. Road connects two distinct buildings and , and traversing takes time. There is at most one road directly connecting a given pair of buildings.
One day, Yun learned that by casting magic he can change the traffic conditions and thus change the time needed to traverse each road. Yun can cast magic at most times, and after casting magic times, the time needed to traverse road becomes for every . Yun can cast magic only while he is in a building, and he cannot cast magic while traversing a road.
Yun wants to use magic appropriately to reach the company in the shortest time. Help Yun find the shortest time needed to reach the company.
Input
The first line of the input contains , , , and .
The next lines contain separated by spaces.
The next line contains .
Of the next lines, the -th line contains separated by spaces.
Output
Print the shortest time needed for Yun to reach the company when he uses magic appropriately.