#FW0000023. Lập lộ trình chuyến đi (Trip Routing)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 . Each of the next 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 . 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 , the route column left-aligned in width , and mileage right-aligned in width , as illustrated in the example.
Subtasks
- Subtask 1 (40 points): at most distinct cities; ; ; ; .
- Subtask 2 (60 points): ; ; ; .
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 miles, cheaper than the -mile direct road. The reverse query is a four-mile road.