## COCI '07 Contest 6 #4 George

View as PDF

Points: 15
Time limit: 0.6s
Memory limit: 32M

Problem type

Last week Mister George visited Croatia. Since Mister George is a very important person, while he was in a street, the police disallowed entry to that street, but vehicles that entered the street before Mister George could continue driving.

While Mister George was visiting, Luka drove his truck around town. But because of some of the streets being closed off, he couldn't make his delivery in time and almost lost his job. Although it is late now, he is wondering how he could have planned his delivery better i.e. what would have been the least time needed to make his delivery while Mister George was visiting. He knows the route mister George took.

The city is modeled with intersections and two-way streets connecting them. For each street, Luka knows how much time he needs to traverse it (Mister George needs the same amount of time).

For example, if Mister George starts traversing a street during minute and needs minutes to exit it, this street will be blocked during minutes and . Luka can enter the street during minutes and earlier, or and later.

Write a program that calculates the least amount of time Luka needs to make his delivery, if he starts driving minutes after the arrival of Mister George.

#### Input Specification

The first line contains two integers and , the number of intersections and the number of streets. The intersections are numbered to .

The second line contains four integers and .

These are, in order:

• The intersection where Luka starts;
• The intersection Luka must get to;
• The difference in starting times between mister George and Luka (Luka starts at intersection exactly minutes after mister George starts his route);
• The number of intersections on Mister George's route.

The third line contains integers, the labels of intersections mister George will visit. Every pair of adjacent integers denotes a street he will traverse. That street will exist and Mister George will traverse every street at most once.

Each of the following lines contains three integers and , meaning that there is a street between intersection and , and it takes minutes to traverse. will be between and .

#### Output Specification

Output the least amount of time (in minutes) Luka needs to make his delivery.

#### Sample Input 1

6 5
1 6 20 4
5 3 2 4
1 2 2
2 3 8
2 4 3
3 6 10
3 5 15

#### Sample Output 1

21

#### Sample Input 2

8 9
1 5 5 5
1 2 3 4 5
1 2 8
2 7 4
2 3 10
6 7 40
3 6 5
6 8 3
4 8 4
4 5 5
3 4 23

#### Sample Output 2

40