D - 安全な通学路 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

高橋君は毎日、自宅から学校まで歩いて通学しています。最近、通学路の途中にある地点の近くで野犬が目撃されるようになりました。野犬は特定の地点を拠点にしており、その周辺をうろついています。

高橋君は、通学路として使う経路を工夫することで、野犬の拠点からできるだけ離れた経路を通りたいと考えています。

街は N 個の地点と M 本の双方向の道で構成されています。各地点には 1 から N までの番号が付いています。各道 i (1 \leq i \leq M) は地点 U_i と地点 V_i を結び、通過するのに W_i 分かかります(W_i は正の整数)。

高橋君の自宅は地点 S、学校は地点 T、野犬の拠点は地点 P です。

野犬の拠点である地点 P からある地点 X への最短所要時間 d(X) を次のように定義します。

  • d(P) = 0 とします。
  • 地点 P から地点 X(X \neq P)に到達可能であるとき、d(X) は地点 P から地点 X へ至る経路における所要時間(通過する道の W_i の合計)の最小値です。
  • 地点 P から地点 X に到達できない場合は d(X) = +\infty とします。

ここで、+\infty は任意の有限の値より大きいものとします。

地点 S から地点 T への単純パスとは、地点の列 v_0, v_1, \ldots, v_k(ただし k \geq 1, v_0 = S, v_k = T)であって、以下の条件をすべて満たすものです。

  • v_0, v_1, \ldots, v_k はすべて相異なる(同じ地点を 2 度以上通らない)。
  • すべての 1 \leq j \leq k に対し、地点 v_{j-1} と地点 v_j を結ぶ道が存在する。

ある単純パス v_0, v_1, \ldots, v_k の安全度を、始点・終点を含む経路上のすべての地点における d の最小値、すなわち

\min(d(v_0), d(v_1), \ldots, d(v_k))

として定義します。ここで、d の値に有限の値と +\infty が混在する場合、\min は有限の値の中の最小値をとります。すべての d(v_j) が +\infty である場合に限り、安全度は +\infty となります。

安全度は、経路上で最も野犬の拠点に近い地点がどれだけ拠点から離れているかを表す指標です。

高橋君が選べる地点 S から地点 T への単純パスのうち、安全度が最大となるものの安全度を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S, T, P \leq N
  • S \neq T
  • S と P、T と P はそれぞれ一致してもよい
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • 同じ 2 地点を結ぶ道は高々 1 本である
  • 地点 S から地点 T へ至る単純パスが少なくとも 1 つ存在する
  • 入力はすべて整数である

入力

N M
S T P
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • 1 行目には、地点の数 N と道の数 M が、スペース区切りで与えられる。
  • 2 行目には、高橋君の自宅の地点番号 S、学校の地点番号 T、野犬の拠点の地点番号 P が、スペース区切りで与えられる。
  • 3 行目から M + 2 行目までの M 行で、各道の情報が与えられる。
  • 2 + i 行目 (1 \leq i \leq M) には、道 i が結ぶ 2 つの地点 U_i, V_i と、通過にかかる時間 W_i が、スペース区切りで与えられる。

出力

高橋君が選べる単純パスにおける安全度の最大値を 1 行で出力せよ。

安全度の最大値が +\infty となる場合、すなわち経路上のすべての地点が地点 P から到達不可能であるような単純パスが存在する場合は、代わりに INF と出力せよ。

それ以外の場合、安全度の最大値は非負整数となる。その値を出力せよ。


入力例 1

5 5
1 5 3
1 2 1
2 3 2
3 5 1
1 4 3
4 5 3

出力例 1

1

入力例 2

4 3
1 2 4
1 2 1
1 3 2
2 3 3

出力例 2

INF

入力例 3

8 10
1 8 4
1 2 3
2 3 2
3 4 1
4 8 5
1 5 2
5 6 4
6 7 3
7 8 2
2 6 1
3 7 6

出力例 3

4

入力例 4

12 16
1 12 6
1 2 3
2 3 4
3 6 2
6 12 1
1 4 5
4 5 3
5 9 2
9 10 4
10 11 3
11 12 2
4 8 6
8 12 7
2 7 1
7 11 5
5 6 8
8 9 1

出力例 4

1

入力例 5

2 1
1 2 1
1 2 1000000000

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi walks from his home to school every day. Recently, a stray dog has been spotted near a certain point along his school route. The stray dog is based at a specific point and roams around its vicinity.

Takahashi wants to choose a route that stays as far away from the stray dog's base as possible.

The town consists of N points and M bidirectional roads. Each point is numbered from 1 to N. Each road i (1 \leq i \leq M) connects point U_i and point V_i, and takes W_i minutes to traverse (W_i is a positive integer).

Takahashi's home is at point S, his school is at point T, and the stray dog's base is at point P.

The shortest travel time d(X) from the stray dog's base at point P to a point X is defined as follows:

  • d(P) = 0.
  • When point X (X \neq P) is reachable from point P, d(X) is the minimum travel time (sum of W_i of the roads traversed) over all paths from point P to point X.
  • If point X is not reachable from point P, then d(X) = +\infty.

Here, +\infty is considered larger than any finite value.

A simple path from point S to point T is a sequence of points v_0, v_1, \ldots, v_k (where k \geq 1, v_0 = S, v_k = T) satisfying all of the following conditions:

  • v_0, v_1, \ldots, v_k are all distinct (no point is visited more than once).
  • For all 1 \leq j \leq k, there exists a road connecting point v_{j-1} and point v_j.

The safety score of a simple path v_0, v_1, \ldots, v_k is defined as the minimum value of d over all points on the path, including the start and end points, namely:

\min(d(v_0), d(v_1), \ldots, d(v_k))

Here, if the values of d include both finite values and +\infty, the \min takes the minimum among the finite values. The safety score is +\infty only when all d(v_j) are +\infty.

The safety score represents how far the closest point to the stray dog's base along the route is from that base.

Among all simple paths from point S to point T that Takahashi can choose, find the maximum safety score.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S, T, P \leq N
  • S \neq T
  • S and P, as well as T and P, may coincide
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • There is at most one road connecting any pair of points
  • There exists at least one simple path from point S to point T
  • All input values are integers

Input

N M
S T P
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • The first line contains the number of points N and the number of roads M, separated by a space.
  • The second line contains the point number S of Takahashi's home, the point number T of his school, and the point number P of the stray dog's base, separated by spaces.
  • The following M lines (from line 3 to line M + 2) give the information of each road.
  • The (2 + i)-th line (1 \leq i \leq M) contains the two points U_i, V_i connected by road i and the traversal time W_i, separated by spaces.

Output

Output the maximum safety score among all simple paths that Takahashi can choose, in a single line.

If the maximum safety score is +\infty, that is, if there exists a simple path where all points on the path are unreachable from point P, output INF instead.

Otherwise, the maximum safety score is a non-negative integer. Output that value.


Sample Input 1

5 5
1 5 3
1 2 1
2 3 2
3 5 1
1 4 3
4 5 3

Sample Output 1

1

Sample Input 2

4 3
1 2 4
1 2 1
1 3 2
2 3 3

Sample Output 2

INF

Sample Input 3

8 10
1 8 4
1 2 3
2 3 2
3 4 1
4 8 5
1 5 2
5 6 4
6 7 3
7 8 2
2 6 1
3 7 6

Sample Output 3

4

Sample Input 4

12 16
1 12 6
1 2 3
2 3 4
3 6 2
6 12 1
1 4 5
4 5 3
5 9 2
9 10 4
10 11 3
11 12 2
4 8 6
8 12 7
2 7 1
7 11 5
5 6 8
8 9 1

Sample Output 4

1

Sample Input 5

2 1
1 2 1
1 2 1000000000

Sample Output 5

0