#P7669. [JOI 2018 Final] 月票购买 / Commuter Pass

[JOI 2018 Final] 月票购买 / Commuter Pass

Description

JOI lives in a city with NN stations, numbered from 11 to NN. There are MM railways, numbered from 11 to MM. Railway ii (1iM1 \leq i \leq M) connects stations AiA_i and BiB_i in both directions, and its fare is CiC_i yen.

JOI lives near station SS and goes to IOI High School near station TT. He plans to buy a commuter pass connecting these two stations. When buying the commuter pass, he must choose a route of minimum total cost between station SS and station TT. With this commuter pass, he can ride any railway included in the chosen route in either direction without any additional cost.

JOI often visits a bookstore near stations UU and VV. Therefore, he wants to buy a commuter pass so that the cost of traveling from station UU to station VV is minimized.

When he travels from station UU to station VV, he first chooses a route from station UU to station VV. Then the fare he needs to pay is:

  • 00 yen if railway ii is included in the route chosen when buying the commuter pass, or
  • CiC_i yen if railway ii is not included in the route chosen when buying the commuter pass.

The total of these fares is the cost from station UU to station VV.

He wants to know the minimum possible cost from station UU to station VV if he chooses an appropriate route when buying the commuter pass.

Input Format

The first line contains two space-separated integers NN and MM, meaning the city has NN stations and MM railways.

The second line contains two space-separated integers SS and TT, meaning JOI plans to buy a commuter pass from station SS to station TT.

The third line contains two space-separated integers UU and VV, meaning JOI wants to minimize the cost from station UU to station VV.

Each of the next MM lines, the ii-th line contains three space-separated integers AiA_i, BiB_i, and CiC_i. Railway ii connects stations AiA_i and BiB_i in both directions, and its fare is CiC_i yen.

Output Format

Output one integer: the minimum cost from station UU to station VV if he chooses an appropriate route when buying the commuter pass.

6 6
1 6
1 4
1 2 1
2 3 1
3 5 1
2 4 3
4 5 2
5 6 1
2
6 5
1 2
3 6
1 2 1000000000 
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
3000000000
8 8
5 7
6 8
1 2 2
2 3 3
3 4 4
1 4 1
1 5 5
2 6 6
3 7 7
4 8 8
15
5 5
1 5
2 3
1 2 1
2 3 10
2 4 10
3 5 10
4 5 10
0
10 15
6 8
7 9
2 7 12
8 10 17
1 3 1
3 8 14
5 7 15
2 3 7
1 10 14
3 6 12
1 5 10
8 9 1
2 9 7
1 4 1  
1 8 1
2 4 7
5 6 16
19

Hint

Constraints

For 100%100\% of the testdata: 2N1052 \leq N \leq 10^5, 1M2×1051 \leq M \leq 2×10^5, 1SN1 \leq S \leq N, 1TN1 \leq T \leq N, 1UN1 \leq U \leq N, 1VN1 \leq V \leq N, S=/TS {=}\mathllap{/\,} T, U=/VU {=}\mathllap{/\,} V, S=/U S{=}\mathllap{/\,} U or T=/VT {=}\mathllap{/\,} V, 1AiBiN1 \leq A_i \leq B_i \leq N (1iM1 \leq i \leq M). For every 1i<jM1 \leq i < j \leq M, Ai=/AjA_i {=}\mathllap{/\,} A_j or Bi=/BjB_i {=}\mathllap{/\,} B_j, 1Ci1091 \leq C_i \leq 10^9 (1iM1 \leq i \leq M). JOI can travel from any station to any other station by railways.

  • Subtask 11 (1616 points): S=US=U.
  • Subtask 22 (1515 points): There is a unique minimum-cost route from station SS to station TT.
  • Subtask 33 (2424 points): N300N \leq 300.
  • Subtask 44 (4545 points): No additional constraints.

Sample Explanation

For Sample 1: When buying the commuter pass, JOI can choose only one route: station 11 → station 22 → station 33 → station 55 → station 66. To minimize the cost from station 11 to station 44, he chooses the route: station 11 → station 22 → station 33 → station 55 → station 44. For this route, the fares he needs to pay are:

  • 22 yen for railway 55 between station 44 and station 55, and
  • 00 yen for all other railways.

For Sample 2: When JOI travels from station 33 to station 66, he does not use the commuter pass.

Problem Source

From The 17th Japanese Olympiad in Informatics (JOI 2017/2018) Final Round, T4: Commuter Pass.
Translated and整理 (pinyin: zhengli, compiled) by @求学的企鹅.

Hack Notes

@gaojunqing provided a set of hack data to the problem setter on November 6, 2021. After multiple checks, it was added to this problem’s testdata on November 7, 2021.
To respect the original JOI problem and its intended experience, all added hack data does not change the scoring: the full score is still 100 points. The new hack data is placed separately in Subtask #5 with a score of 0 points. That is, if you pass all original testdata but fail the hack data, you will get 100 points but will not be accepted (AC). You will be accepted (AC) only if you pass all testdata.

Translated by ChatGPT 5