#P15816. [JOI 2015 Final] 鉄道旅行
[JOI 2015 Final] 鉄道旅行
Problem Description
In JOI Country, there are cities, numbered . Also, there are railways, numbered . Railway () connects City and City in both directions.
There are two ways to ride trains in JOI Country: using paper tickets and using an IC card.
- When riding Railway , if you use a paper ticket, the fare is yen.
- When riding Railway , if you use an IC card, the fare is yen. However, to use an IC card on Railway , you must buy in advance an IC card that can be used on Railway . Buying one IC card usable on Railway costs yen. Once purchased, this IC card can be used unlimited times.
Because IC cards are easier for handling money, the fare with an IC card is cheaper than the fare with a paper ticket. That is, for all , holds. The IC card specifications are completely different for each railway, so for any , an IC card usable on Railway cannot be used on any other railway.
You plan to travel around JOI Country. You will start from City , and visit cities in the order . The trip consists of days. On Day (), you will travel by rail from City to City . At this time, you may need to transfer across multiple railways. Also, you may visit the same city multiple times. Railways in JOI Country are very fast, so you can reach any city from any other city within one day.
Currently, you do not have any IC cards for any railway. You want to buy some railway IC cards in advance, so that the total cost of this trip (the sum of IC card purchase costs and riding fares) is as small as possible.
Task
Given the number of cities in JOI Country, the travel plan, and the fares and IC card prices for each railway, write a program to compute the minimum possible total cost of the trip.
Input Format
Read the following from standard input.
- The first line contains two integers separated by spaces. They represent that JOI Country has cities, and the trip has days.
- The second line contains integers separated by spaces. They mean that on Day (), you will go from City to City .
- In the next lines, the -th line () contains three integers separated by spaces. They mean that on Railway , the paper-ticket fare is yen, the IC-card fare is yen, and the price to buy an IC card usable on Railway is yen.
Output Format
Output one line to standard output containing one integer, the minimum total cost of the trip (in yen).
4 4
1 3 2 4
120 90 100
110 50 80
250 70 130
550
8 5
7 5 3 5 4
12 5 8
16 2 1
3 1 5
17 12 17
19 7 5
12 2 19
4 1 3
81
Hint
Sample Explanation 1
In this case, the plan that minimizes the total cost is as follows:
- Buy IC cards for Railway 2 and Railway 3. This costs yen.
- Day 1: travel from City 1 to City 2 using a paper ticket, then from City 2 to City 3 using an IC card. This costs yen.
- Day 2: travel from City 3 to City 2 using an IC card. This costs yen.
- Day 3: travel from City 2 to City 3 using an IC card, then from City 3 to City 4 using an IC card. This costs yen.
With this movement, the total cost is yen. This is the minimum, so output .
Constraints
All input data satisfy the following conditions:
- .
- .
- ()。
- ()。
- ()。
- ()。
Subtasks
Subtask 1 [20 points]
Satisfies the following conditions:
- 。
- 。
- ()。
- ()。
Subtask 2 [30 points]
Satisfies the following conditions:
- 。
- 。
- ()。
- ()。
Subtask 3 [50 points]
No additional constraints.
Translated by DeepSeek V3.2.
Translated by ChatGPT 5