This page is still under construction.

Parts of this page are still being built. What you see may change.

Mr. Rito's Post Office

Time limit8sMemory limit512 MB

Summary
Given a graph with land and sea edges and a fixed delivery order, find the shortest time to deliver in order while tracking the boat's location, since sea travel requires the boat.
Level

Hard8 of 10

Topics
Dynamic programming, Shortest path, Graph, Implementation
Solved
No attempts yet

Problem

You are a programmer working at a post office on a remote island. The region you live in consists of several islands. Each island has one or more harbor towns. There may be other towns and villages as well. To travel from one island to another, you must use a boat. Within a single island you can use land routes, but sea routes are sometimes faster.

Following the recent privatization of the postal service, postal workers were laid off nationwide to cut costs. The post office on the remote island was no exception, and in the end Mr. Rito was the only mail carrier left. The area his post office covers is very large, so delivering the mail alone is hard work. He has asked you for help figuring out how to deliver the mail efficiently.

Your job is to write a program that finds the shortest tour route given the delivery order of towns and villages that Mr. Rito must follow.

Mr. Rito can never perform deliveries in any order other than the one specified. However, when moving from one town or village to another, he is allowed to pass through other towns and villages. He also has one boat for traveling between islands.

For example, if the delivery order is town A, town B, village C, then when going from town A to town B he may pass through any town or village. He may pass through village C at this point, but to respect the delivery order he must first go to town B and deliver there, then visit village C and deliver there. Also, if he goes from town A to town B by sea and then from town B to village C by land, the boat is left at town B. Therefore, if he wants to use a sea route next, he must return to town B.

There are cases where he must deliver multiple times at the same town or village. For example, the delivery order might be town A, village B, town C, village B. In this case, if he goes from town A to town C without visiting village B, he cannot deliver at town C right away, because the first delivery at village B is not finished. Even if he visits village B and delivers after finishing the delivery at town C, that does not count as finishing the first delivery at village B.

Initially, Mr. Rito is always at some harbor town together with his boat. He is a veteran, so the time spent on delivery work other than travel can be ignored. Only the time until the delivery work at the last town or village is finished matters, and the time to return the boat to its original position and go back to the post office is not considered.

Input

The input consists of multiple data sets. The format of each data set is as follows.

N M
x1 y1 t1 sl1
x2 y2 t2 sl2
...
xM yM tM slM
R
z1 z2 ... zR

All input items in a data set are non-negative integers. Items on a line are separated by a single space.

The first line defines the size of the land and sea route network.

N (2 ≤ N ≤ 200) is the number of towns or villages. Each town or village is assigned a unique number from 1 to N. M (1 ≤ M ≤ 10000) is the total number of land routes and sea routes.

Lines 2 through 1 + M describe land routes and sea routes. xi and yi (1 ≤ xi, yi ≤ N) are the numbers of the towns or villages at the two ends. ti (1 ≤ ti ≤ 1000) is the travel time of that land route or sea route. sli is either 'L' or 'S', where L means a land route and S means a sea route.

There may be two or more land routes or sea routes directly connecting the same pair of towns or villages. Each land route and sea route is bidirectional, that is, it can be traveled in either direction.

R (1 ≤ R ≤ 1000) on line M + 2 is the number of delivery destinations Mr. Rito is responsible for. Line M + 3 contains R numbers zi (1 ≤ zi ≤ N) of the delivery destination towns or villages, listed in delivery order.

In the initial state, Mr. Rito and the boat are both at harbor town z1. From the initial state, it is always possible to reach the delivery destination towns or villages by some means.

The end of the input is indicated by a line containing two zeros separated by a space.

Output

For each data set in the input, find the shortest travel time required for Mr. Rito to tour the towns and villages in the given delivery order, and output it on one line.

Examples1

  1. Example 1

    Input
    3 3
    1 2 5 L
    1 2 7 S
    2 3 11 S
    3
    1 2 3
    5 5
    1 2 15 L
    2 3 10 L
    4 5 7 L
    1 3 30 S
    3 4 100 S
    5
    1 3 5 4 1
    0 0
    
    Expected output
    18
    269