Revenge
Time limit1sMemory limit1024 MB
Each edge in an index range can be crossed for its cost or skipped for its rejection cost; find the minimum cost from u to v for each query range.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Matrix, Graph
- Solved
- No attempts yet
Statement
Gigel has an undirected graph with nodes and edges with positive costs. After the mess Gigel got into at the Romanian National Olympiad in Informatics, Ninel, Gigel's little brother, stole all his edges. Gigel wants to get the edges back, but Ninel makes him go through some challenges. You are given an array of undirected edges of length . Every edge has a regular cost and a rejection cost . Gigel must complete a mission from Ninel: find the minimum cost of going from node to node using a subarray of edges of . The allowed edges are given by an interval with , which gives the indices of the edges in Gigel may use.
Gigel starts at node and iterates over the edges . At each step he does one of two things. He can use the current edge when he is at node to move to node (or the other way around, from to ), and the travel cost increases by the cost of the edge. Or he can reject the current edge and stay at his current node, and the travel cost increases by the rejection cost of the edge.
You know , the array , and missions. Each edge in is a tuple for an edge with cost and rejection cost . Each mission is a tuple : Gigel starts at , must reach , and may only use the edges with indices between and . Find the minimum cost for each mission. If Gigel cannot reach , output .
Input
The first line contains three integers , , and . The next lines each contain four integers , , , describing the edges in . The next lines each contain four integers , , , describing Gigel's missions.
Output
Print lines. The -th line contains the answer for the -th mission.
Constraints
, , , , .