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

#FW0000027. Chi phí vận chuyển thấp nhất (Minimum Transport Cost)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chi phí vận chuyển thấp nhất (Minimum Transport Cost)

Minimum Transport Cost

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A directed transport-cost matrix gives each road price, with −1-1 for no road. Passing through an intermediate city incurs that city’s tax, but trip endpoints are not taxed. For a journey from ss to tt, add the road costs along its directed route and the taxes at every intermediate city, excluding both endpoints. Find the minimum total expense and output a corresponding route. When more than one route is optimal, any optimal route is valid. Every query has a reachable destination in this PHOJ edition.

Input

The first line contains the number of cities nn. The next nn lines each contain nn integers specifying the directed road-cost matrix; an entry of −1-1 means that no direct road exists, while a nonnegative value is the direct cost.

The next line gives the nn taxes for intermediate cities 1,2,…,n1,2,\ldots,n. Finally, read the query count qq and the qq ordered pairs s,ts,t.

Output

For each query print exactly three lines: From s to t : with the requested endpoints; Path: s-->...-->t listing every visited city separated by -->; and Total cost : C, where CC includes all road costs and the intermediate-city taxes. Separate the outputs for consecutive queries by one blank line.

Subtasks

  • Subtask 1 (40 points): n≤7n\le 7; 2≤n≤1002\le n\le 100; 1≤q≤1001\le q\le 100; −1≤wij≤100000-1\le w_ij\le 100000; 0≤taxi≤1000000\le tax_i\le 100000; every query is reachable.
  • Subtask 2 (60 points): 2≤n≤1002\le n\le 100; 1≤q≤1001\le q\le 100; −1≤wij≤100000-1\le w_ij\le 100000; 0≤taxi≤1000000\le tax_i\le 100000; every query is reachable.

Examples

Input

3
0 2 20
-1 0 3
-1 -1 0
1 5 1
3
1 3
1 2
3 3

Output

From 1 to 3 :
Path: 1-->2-->3
Total cost : 10

From 1 to 2 :
Path: 1-->2
Total cost : 2

From 3 to 3 :
Path: 3
Total cost : 0

Explanation

The route 1→2→31\to2\to3 costs 2+5+3=102+5+3=10 including the intermediate tax. Going directly costs 2020. There is no endpoint tax.