Under construction

Still being built. Things may change

Revenge

Time limit1sMemory limit1024 MB

Summary
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 GG with NN nodes and MM 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 SS of length LL. Every edge has a regular cost and a rejection cost rr. Gigel must complete a mission from Ninel: find the minimum cost of going from node uu to node vv using a subarray of edges of SS. The allowed edges are given by an interval [a,b][a,b] with a≤ba \le b, which gives the indices of the edges in SS Gigel may use.

Gigel starts at node xx and iterates over the edges Sa,Sa+1,…,SbS_a, S_{a+1}, \dots, S_b. At each step he does one of two things. He can use the current edge (x,y)(x,y) when he is at node xx to move to node yy (or the other way around, from yy to xx), 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 NN, the array SS, and QQ missions. Each edge in SS is a tuple ⟨x,y,c,r⟩\langle x,y,c,r \rangle for an edge (x,y)(x,y) with cost cc and rejection cost rr. Each mission is a tuple ⟨u,v,a,b⟩\langle u,v,a,b \rangle: Gigel starts at uu, must reach vv, and may only use the edges with indices between aa and bb. Find the minimum cost for each mission. If Gigel cannot reach vv, output −1-1.

Input

The first line contains three integers NN, LL, and QQ. The next LL lines each contain four integers xx, yy, cc, rr describing the edges in SS. The next QQ lines each contain four integers uu, vv, aa, bb describing Gigel's missions.

Output

Print QQ lines. The ii-th line contains the answer for the ii-th mission.

Constraints

2≤N≤302 \le N \le 30, 1≤L≤3×1041 \le L \le 3 \times 10^4, 1≤Q≤3×1051 \le Q \le 3 \times 10^5, 0≤c,r≤1040 \le c, r \le 10^4, 1≤x,y,u,v≤N1 \le x, y, u, v \le N.

Examples2

  1. Example 1

    Input
    5 5 3
    1 4 4 5
    4 1 6 1
    2 1 2 9
    2 5 1 0
    1 5 2 5
    2 2 2 4
    5 4 5 5
    1 5 2 5
    
    Expected output
    10
    -1
    9
    
  2. Example 2

    Input
    4 8 6
    2 4 5 8
    2 4 4 8
    2 3 6 4
    1 4 5 0
    2 4 10 10
    1 3 5 2
    3 2 2 9
    3 4 1 1
    3 2 1 5
    3 1 2 2
    1 1 1 7
    2 3 2 4
    3 3 1 7
    1 2 2 5
    
    Expected output
    32
    -1
    41
    14
    36
    27