#P6096. [JSOI2015] 地铁线路

[JSOI2015] 地铁线路

Description

The subway in the JSOI Kingdom has become more expensive again. JYY, who is traveling in JSOI, is very unhappy. After this fare reform, passengers are not charged by travel distance, but by the number of transfers. JYY also needs to update his route search software accordingly. JYY thinks: if he pays the same fare, wouldn’t it be better for him if he can ride through more stations? So JYY wants to develop a route search program so that he can always “profit” the most.

The JSOI subway has NN lines and SS stations. Line ii contains LiL_i stations. Each subway line is a straight line from its starting station to its terminal station (there are no weird circular lines like Beijing Subway Line 2 or Line 10). Also, each line runs in both directions. If different lines pass through the same station, passengers can transfer at that station. Under the latest JSOI subway fare rule, every time a passenger enters a running subway train, they must pay a fee of 11. Therefore, if a passenger transfers xx times in total, they need to pay x+1x+1 fares. Since each line runs in both directions, at any station you may transfer to the opposite-direction train of the same line. Note that even transferring to the opposite direction of the same line still requires paying (because you always need to get off and then get on again).

Now JYY wants to take the subway from station AA to station BB. Assume that for any subway line, the travel time between two adjacent stations is 11 minute, and stopping at stations and transferring both take no time. JYY wants to know:

  1. The minimum fare he needs to pay.
  2. Under the minimum fare, the maximum number of minutes he can spend riding the subway.

Input Format

The first line contains two positive integers NN and SS.

The second line contains SS space-separated strings, the names of the SS stations.

The next NN lines each describe one subway line. For line ii, the line first contains a positive integer LiL_i, followed by LiL_i strings, the station names on this line. A line is allowed to stop at the same station multiple times.

Line N+3N+3 contains two different strings AA and BB, meaning JYY is currently at station AA and wants to go to station BB.

Output Format

The first line contains an integer CC, the minimum fare JYY needs to pay.

The second line contains an integer TT, the longest time he can ride the subway while spending CC.

If there is no route between the two stations, output -1 on the first line and 0 on the second line.

2 5
A B C D E
4 A B C D
3 C D E
A D
1
3

Hint

For 100%100\% of the testdata, 2N4×1052\leq N\leq 4\times 10^5, S3×105S\leq 3\times 10^5, Li8×105\sum L_i\leq 8\times 10^5.

It is guaranteed that the length of every input string does not exceed 4040, and each string contains only letters, digits, and hyphen -.

Translated by ChatGPT 5