#P3686. [CERC2016] 爵士之旅 Jazz Journey
[CERC2016] 爵士之旅 Jazz Journey
Description
Ivan is planning a grand European tour for his jazz band. There are cities in Europe, labeled . Ivan plans to hold concerts in cities in this exact order, and he will never perform in the same city twice in a row (that is, ). However, over the whole tour, he may visit the same city multiple times. In the end, he will return to the starting city to perform (that is, ).
Each time, Ivan takes a direct flight from to . He wants to be smart and minimize the total cost of tickets. As you know, prices depend on supply and demand: for example, a one-way ticket might even be more expensive than a round-trip ticket for the same pair of cities.
There are two types of tickets available:
-
A one-way ticket from to , which allows exactly one flight from to , but not from to .
-
A round-trip ticket from to : buying one allows one flight from to , and then one flight from back to , but you are not allowed to use it in the reverse order. Of course, you may choose not to fly back after going from to .
You are given the set of available tickets, with unlimited supply of each type. Please help Ivan find the minimum possible total cost. You may assume that at least one valid plan exists.
Input Format
The first line contains two positive integers (), the number of cities and the number of concerts.
The second line contains positive integers (), indicating the city of each concert in order.
The next line contains a positive integer (), the number of ticket types.
Each of the next lines contains two positive integers (), the start and end cities; followed by a character indicating the ticket type, where "O" means a one-way ticket and "R" means a round-trip ticket; and finally a positive integer (), the ticket price.
Output Format
Output a single integer: the minimum total ticket cost to complete the tour.
2 5
1 2 1 2 1
4
1 2 R 6
1 2 O 3
2 1 O 3
1 2 R 5
10
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号