Revenge

시간 제한1초메모리 제한1024 MB

요약
각 질의마다 인덱스 구간 [a,b]의 간선만 사용해 u에서 v로 가는 최소 비용을 구한다. 간선을 건너뛰면 거부 비용이 든다.
난이도

어려움10점 중 8점

유형
동적 계획법, 최단 경로, 세그먼트 트리, 행렬
정답자
아직 제출이 없습니다

문제

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 is going to make him go through some challenges.

You are given an array of undirected edges SS of length LL. Every edge has a regular cost, but it also has a rejection cost rr. Gigel has to accomplish the following mission he got from Ninel: find the minimum cost of going from node uu to node vv using a subarray of edges of SS. Gigel is given an interval \[a,b]\(a≤b) which determines the indices of the edges in SS he is allowed to use.

Gigel is initially in node xx and he iterates over the edges S_a,S_a+1,…,S_bS\_a,S\_{a+1},\dots ,S\_b. At each step:

  • He chooses to use the current edge (x,y)(x,y) if he currently is in node xx to move to node yy (or the other way around, if he's in node yy to move to node xx). The travelling cost is increased by the cost of the edge (x,y)(x,y).
  • He rejects the current edge and doesn't move from his current node. The travelling cost is increased by the rejection cost of the edge.

You know the number of nodes NN, the array of edges SS and QQ missions Gigel needs to accomplish.

The array SS consists of tuples of the form:

  • \<x,y,c,r>\<x,y,c,r>, representing an edge (x,y)(x,y) with cost cc and rejection cost rr

The QQ missions are tuples of the form:

  • \<u,v,a,b>\<u,v,a,b>: Gigel is initially in node uu and has to move to node vv, using the edges with indices between aa and bb.

Find the minimum cost for each mission. If Gigel cannot reach node vv output −1-1.

입력

The first line contains three integers NN, LL and QQ.

The next LL lines contain four integers x,y,c,rx,y,c,r corresponding to the edges in SS.

The next QQ lines contain four integers u,v,a,bu,v,a,b corresponding to Gigel's missions.

출력

Print QQ lines, each containing the answer for one of Gigel's missions, in the given order.

제한

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

예제2

  1. 예제 1

    입력
    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
    
    예상 출력
    10
    -1
    9
    
  2. 예제 2

    입력
    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
    
    예상 출력
    32
    -1
    41
    14
    36
    27