1634.Volleyball

Time Limit: 1s Memory Limit: 256MB

Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has $n$ junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.

Initially each junction has exactly one taxi standing there. The taxi driver from the $i$-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than $t_i$ meters. Also, the cost of the ride doesn't depend on the distance and is equal to $c_i$ bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.

At the moment Petya is located on the junction $x$ and the volleyball stadium is on the junction $y$. Determine the minimum amount of money Petya will need to drive to the stadium.

Input Format(From the terminal/stdin)

The first line contains two integers $n$ and $m (1 \le n \le 1000,0 \le m \le 1000)$. They are the number of junctions and roads in the city correspondingly. The junctions are numbered from $1$ to $n$, inclusive. The next line contains two integers $x$ and $y (1 \le x,y \le n)$. They are the numbers of the initial and final junctions correspondingly. Next $m$ lines contain the roads' description. Each road is described by a group of three integers $u_i, v_i, w_i (1 \le u_i,v_i \le n,1 \le w_i \le 10^9)$ - they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next $n$ lines contain $n$ pairs of integers $t_i$ and $c_i (1 \le t_i,c_i \le 10^9)$, which describe the taxi driver that waits at the $i$-th junction - the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.

Output Format(To the terminal/stdout)

If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.

Sample Input

Copy
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
 · \n
 · \n
 · · \n
 · · \n
 · · \n
 · · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
9
 \n

Hints

An optimal way - ride from the junction $1$ to $2$ (via junction $4$), then from $2$ to $3$. It costs $7+2=9$ bourles.

Submit

请先 登录

© 2025 FAQs