Two Roads
InterviewTime limit3sMemory limit1024 MB
Given a directed graph, for each of Q scenarios that add two one-way roads, find the shortest time from home S to office T, or -1 if unreachable.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Ulsan has intersections numbered from to , and roads connecting them. Each road is one-way, and the time needed to travel along it is fixed. Yoon drives every day from home to the office in a car with Hyundai Mobis's self-driving system. Yoon's home is at intersection , and the office is at intersection . The self-driving system always uses a route that takes the shortest time from home to the office. Yoon recently heard that two new roads will be built in Ulsan. Yoon expects to reach the office faster, but Yoon does not know which intersections the new roads connect or how long they take. So Yoon assumes road construction scenarios and wants to calculate the route the car would take in each case. For each scenario, find the time Yoon needs to drive from home to the office using the self-driving system.
Input
The first line contains the number of intersections , the number of existing roads , the home intersection , and the office intersection , separated by spaces. (; ; ; )
The next lines each contain three integers , , separated by spaces. They describe a one-way road from intersection to intersection that takes time to traverse. (; )
The next line contains the number of construction scenarios . ()
The next lines each contain six integers , , , , , separated by spaces. They describe a scenario in which two new one-way roads are built: one from intersection to taking time, and one from intersection to taking time. (; )
The same pair of intersections may be connected by several roads, and a road may start and end at the same intersection.
Output
For each construction scenario, print on its own line the time Yoon needs to drive from home to the office. If the trip is impossible in a scenario, print on that line.