Regular checkup
Time limit1.5sMemory limit256 MB
In a graph split by a river with B bridges, answer Q queries for the shortest travel time from a given house to a given hospital, or -1 if unreachable.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
The residents of DD Planet travel to a hospital for a regular checkup.
DD Planet has houses (numbered ) and hospitals (numbered ). A deep river separates the area holding the houses from the area holding the hospitals. Crossing the river is possible only over a bridge. There are bridges (numbered ), and crossing any bridge takes seconds.
DD Planet also has roads that join houses, hospitals, and bridges. A road between two bridges is allowed. Because the deep river runs between the two areas, no road joins a house directly to a hospital.
Answer questions.
- What is the minimum time for a resident of house to reach hospital ?
Input
The first line contains the number of houses and the number of hospitals (), the number of bridges (), the number of roads (), and the number of questions (), separated by spaces.
Each of the next lines contains three integers , , and , separated by spaces. A road joins point and point , and travelling along that road takes () time.
Each of the next lines contains and for one question, separated by spaces. (, )
Output
On line , print the answer to question . If house cannot reach hospital , print .