This page is still under construction.

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

Magical Bridges

Time limit8sMemory limit256 MB

Summary
Choose one shared length for all magical bridges so the shortest routes from two starts to the target differ as little as possible.
Level

Hard8 of 10

Topics
Shortest path, Math
Solved
No attempts yet

Problem

A magician lives in a country made of NN islands and MM bridges. Some of the bridges are magical bridges that the magician built. Her magic changes the lengths of all magical bridges at once to the same value, which must be a non-negative integer.

The country has a famous race played by two people. Player 1 starts from island S1S_1 and player 2 starts from island S2S_2. Whoever reaches island TT first wins.

The magician enjoys watching this race, so she sets the length of the magical bridges to make the race as close as possible: she minimizes the difference between the shortest-path distance from S1S_1 to TT and the shortest-path distance from S2S_2 to TT. Movement inside an island does not count.

Compute how small that difference can be.

The magician picks the length once before the race starts and never changes it during the race. All magical bridges share the same length. Every bridge can be crossed in either direction.

Input

The input holds several datasets. Each dataset has the format below. Every number in the input is an integer.

N M S1 S2 T
a1 b1 w1
a2 b2 w2
...
aM bM wM

(ai,bi)(a_i, b_i) means bridge ii connects island aia_i and island bib_i.

wiw_i is either a non-negative integer or the letter x. If wiw_i is an integer, bridge ii is a normal bridge of length wiw_i. If wiw_i is x, bridge ii is a magical bridge.

  • 1≤N≤10001 \le N \le 1000
  • 1≤M≤20001 \le M \le 2000
  • 1≤S1,S2,T≤N1 \le S_1, S_2, T \le N
  • S1S_1, S2S_2, TT are all different.
  • 1≤ai,bi≤N1 \le a_i, b_i \le N
  • ai≠bia_i \neq b_i
  • 0≤wi≤10000000000 \le w_i \le 1000000000 for every normal bridge ii
  • The number of magical bridges is at most 100.
  • TT is reachable from S1S_1 and from S2S_2.

The line after the last dataset holds five zeros separated by single spaces. That line marks the end of the input and is not processed.

Output

For each dataset, print the smallest possible difference between the two shortest-path distances on its own line.

Examples5

  1. Example 1

    Input
    3 2 2 3 1
    1 2 1
    1 3 2
    4 3 1 4 2
    2 1 3
    2 3 x
    4 3 x
    0 0 0 0 0
    
    Expected output
    1
    1
  2. Example 2

    Input
    3 2 2 3 1
    1 2 5
    1 3 5
    3 2 2 3 1
    1 2 0
    1 3 1000000000
    0 0 0 0 0
    
    Expected output
    0
    1000000000
  3. Example 3

    Input
    4 3 2 3 1
    1 2 x
    1 4 x
    4 3 x
    0 0 0 0 0
    
    Expected output
    0
  4. Example 4

    Input
    6 7 2 3 1
    2 1 20
    2 4 x
    4 1 3
    3 1 8
    3 5 x
    5 6 x
    6 1 x
    0 0 0 0 0
    
    Expected output
    0
  5. Example 5

    Input
    4 6 4 2 1
    1 2 x
    2 3 x
    3 4 6
    1 4 1
    3 2 x
    2 4 0
    6 9 1 5 6
    1 2 6
    2 3 2
    3 4 8
    2 5 x
    2 6 x
    4 6 0
    5 2 5
    1 4 3
    3 2 4
    6 7 2 6 5
    1 2 1
    2 3 1
    1 4 6
    1 5 0
    1 6 5
    5 3 x
    3 4 7
    7 9 7 5 4
    1 2 x
    1 3 0
    3 4 7
    2 5 7
    1 6 6
    2 7 0
    4 3 x
    3 7 2
    3 4 x
    3 5 2 3 1
    1 2 3
    1 3 x
    2 3 x
    2 1 7
    1 3 4
    0 0 0 0 0
    
    Expected output
    0
    1
    4
    7
    0