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

#FW0000078. Chi phí đường đi và tiệc (Asterix and Obelix)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chi phí đường đi và tiệc (Asterix and Obelix)

Asterix and Obelix

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Every undirected road has a travel cost and every city has a feast cost. An itinerary costs the sum of its road costs plus the maximum feast cost over all visited cities, including its endpoints. Answer minimum-cost queries.

Input

First n,m,qn,m,q; then nn feast costs, mm undirected roads u,v,wu,v,w, and qq queries s,ts,t, all 1-based.

Output

For each query print the minimum cost or -1 if disconnected.

Subtasks

  • Subtask 1 (40 points): 2≤n≤7, 1≤q≤122\le n\le 7,\ 1\le q\le 12. 0≤m≤10000\le m\le1000, 1≤feei,w≤1061\le fee_i,w\le10^6; 1≤s,t≤n1\le s,t\le n.

  • Subtask 2 (60 points): 2≤n≤30, 1≤q≤302\le n\le 30,\ 1\le q\le 30. 0≤m≤10000\le m\le1000, 1≤feei,w≤1061\le fee_i,w\le10^6; 1≤s,t≤n1\le s,t\le n.

Examples

Input

3 2 3
5 10 20
1 2 2
2 3 3
1 3
1 2
3 3

Output

25
12
20

Explanation

The route 1–2–3 costs 5 in road expenses and has a maximum feast cost of 20, totaling 25. The route 1–2 costs 12; staying at 3 costs its feast fee of 20.