PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000001. Đường đi ngắn nhất II (Shortest Routes II)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi ngắn nhất II (Shortest Routes II)

Shortest Routes II

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given a weighted undirected road network, answer shortest-distance queries between city pairs. Return −1-1 if disconnected. Parallel roads are possible.

Input

n,m,qn,m,q, then mm roads u,v,wu, v, w and qq pairs u,vu, v.

Output

For each query print the shortest distance or -1.

Subtasks

  • Subtask 1 (40 points): n≤8n\le 8, q≤12q\le 12; 1≤n≤5001\le n\le 500; 1≤m≤n∗n1\le m\le n*n; 1≤q≤1000001\le q\le 100000; 1≤u,v≤n1\le u,v\le n; 1≤w≤10000000001\le w\le 1000000000.
  • Subtask 2 (60 points): 1≤n≤5001\le n\le 500; 1≤m≤n∗n1\le m\le n*n; 1≤q≤1000001\le q\le 100000; 1≤u,v≤n1\le u,v\le n; 1≤w≤10000000001\le w\le 1000000000.

Examples

Input

4 3 5
1 2 5
1 3 9
2 3 3
1 2
2 1
1 3
1 4
3 2

Output

5
5
8
-1
3

Explanation

The route 1→2→31\to2\to3 costs 5+3=85+3=8, less than the direct edge of length 99. City 44 is disconnected, so the query to it returns -1. Roads are bidirectional.