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

#FW0000023. Lập lộ trình chuyến đi (Trip Routing)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lập lộ trình chuyến đi (Trip Routing)

Trip Routing

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Roads connect named cities and carry a route name and length in miles. For each request, display a minimum-mileage itinerary with its intermediate road segments, road names, distances and total mileage in a fixed-width table. City names may contain spaces but not commas, because commas separate the input fields. Roads are bidirectional. An itinerary may consist of several consecutive roads and its total distance is the sum of their lengths. Any minimum-length itinerary is accepted. Every requested pair has a route in this PHOJ edition.

Input

The first line contains the number of road entries mm. Each of the next mm lines has the format CityA,CityB,Route,Miles: the two endpoint city names, the road name and the positive integer number of miles.

Next comes the query count qq. Each query line is CityA,CityB and may include spaces in city names.

Output

For each query, output a route table headed From, To, Route, Miles, then a divider. List one road segment per row in travel order, and finish with a divider and a Total row for the overall mileage. Separate consecutive query tables with one blank line.

Format the first two columns left-aligned in fields of width 2121, the route column left-aligned in width 1111, and mileage right-aligned in width 55, as illustrated in the example.

Subtasks

  • Subtask 1 (40 points): at most 88 distinct cities; 1≤m≤10001\le m\le 1000; 1≤q≤1001\le q\le 100; 1≤Miles≤1000001\le Miles\le 100000; citycount≤100city_count\le 100.
  • Subtask 2 (60 points): 1≤m≤10001\le m\le 1000; 1≤q≤1001\le q\le 100; 1≤Miles≤1000001\le Miles\le 100000; citycount≤100city_count\le 100.

Examples

Input

3
A City,B City,R1,4
B City,C City,R2,3
A City,C City,R3,10
2
A City,C City
B City,A City

Output

From                 To                   Route      Miles
-------------------- -------------------- ---------- -----
A City               B City               R1             4
B City               C City               R2             3
                                                     -----
                                                Total    7

From                 To                   Route      Miles
-------------------- -------------------- ---------- -----
B City               A City               R1             4
                                                     -----
                                                Total    4

Explanation

The itinerary A City–B City–C City uses 4+3=74+3=7 miles, cheaper than the 1010-mile direct road. The reverse query is a four-mile road.