Drifting

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

요약
특정 두 번의 이동 조합이 금지된 조건에서 정점 N에 도달할 수 있는지, 도달한다면 지나온 간선 가중치 합의 최솟값을 구한다.
난이도

보통10점 중 7점

유형
그래프, 최단 경로, 동적 계획법
정답자
아직 제출이 없습니다

문제

You are given a weighted directed graph of NN vertices and MM edges, with vertices numbered 11 to NN and edges numbered 11 to MM. The ii-th (1≤i≤M1 \le i \le M) edge connects from vertex u_iu\_i to vertex v_iv\_i (u_i<v_iu\_i < v\_i), and the weight of the edge is w_iw\_i.

Also, KK triplets of integers are given. The ii-th (1≤i≤K1 \le i \le K) triplet is (a_i,b_i,c_i)(a\_i, b\_i, c\_i) (a_i<b_i<c_ia\_i < b\_i < c\_i).

You start at vertex 11 and move to vertex NN by repeatedly moving along an edge.

In addition, for all ii (1≤i≤K1 \le i \le K), if you move from vertex a_ia\_i to vertex b_ib\_i directly, we must next move to a vertex other than vertex c_ic\_i.

Judge whether it is possible to reach vertex NN. If it is possible to reach, also calculate the minimum sum of the weights of the edges you pass through.

입력

NN MM

u_1u\_1 v_1v\_1 w_1w\_1

u_2u\_2 v_2v\_2 w_2w\_2

⋮\vdots

u_Mu\_M v_Mv\_M w_Mw\_M

KK

a_1a\_1 b_1b\_1 c_1c\_1

a_2a\_2 b_2b\_2 c_2c\_2

⋮\vdots

a_Ka\_K b_Kb\_K c_Kc\_K

출력

If you cannot reach vertex NN, output −1-1. Otherwise, output the minimum sum of the weights of the edges you pass through.

제한

  • All inputs consist of integers.
  • 3≤N≤2×1053 \le N \le 2 \times 10^5
  • 0≤M≤2×1050 \le M \le 2 \times 10^5
  • 1≤u_i<v_i≤N1 \le u\_i < v\_i \le N (1≤i≤M1 \le i \le M)
  • i≠j⇒(u_i,v_i)≠(u_j,v_j)i \ne j \Rightarrow (u\_i, v\_i) \ne (u\_j, v\_j) (1≤i,j≤M1 \le i, j \le M)
  • 1≤w_i≤1091 \le w\_i \le 10^9 (1≤i≤M1 \le i \le M)
  • 0≤K≤2×1050 \le K \le 2 \times 10^5
  • 1≤a_i<b_i<c_i≤N1 \le a\_i < b\_i < c\_i \le N (1≤i≤K1 \le i \le K)

힌트

In Sample Input 1, the best move is 1→3→41 \rightarrow 3 \rightarrow 4.

In Sample Input 2, the best move is 1→2→4→6→71 \rightarrow 2 \rightarrow 4 \rightarrow 6 \rightarrow 7.

예제3

  1. 예제 1

    입력
    4 4
    1 2 1
    1 3 2
    2 4 2
    3 4 2
    1
    1 2 4
    
    예상 출력
    4
    
  2. 예제 2

    입력
    7 8
    1 2 5
    1 3 2
    2 4 1
    3 4 1
    4 5 6
    4 6 2
    5 7 1
    6 7 1
    2
    2 4 5
    3 4 6
    
    예상 출력
    9
    
  3. 예제 3

    입력
    3 2
    1 2 1
    2 3 1
    1
    1 2 3
    
    예상 출력
    -1