Commuter Pass
Time limit2sMemory limit256 MB
Pick a shortest S-T path to make free, then find the minimum U-V travel cost, where edges on that path cost 0 and others cost their fare.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
JOI lives in a city with stations, numbered from 1 to . The city has railways, numbered from 1 to . Railway () connects station and station in both directions, and its fare is yen.
JOI lives near station and attends IOI High School near station . He plans to buy a commuter pass between these two stations. When he buys the pass, he must choose one route between station and station whose cost is minimum. With this pass he can ride every railway on the chosen route in either direction at no extra charge.
JOI also often visits bookstores near station and station . He therefore wants to buy the pass so that the cost of traveling from station to station is minimized.
To travel from station to station , he first chooses a route from station to station . For each railway on that route he pays
- 0 yen if railway is on the route chosen when he bought the pass, or
- yen if railway is not on the route chosen when he bought the pass.
The sum of these fares is the cost from station to station .
Write a program that computes the minimum cost from station to station when the route for the commuter pass is chosen appropriately.
Input
Read the following data from standard input.
- The first line contains two integers and separated by a space. The city JOI lives in has stations and railways.
- The second line contains two integers and separated by a space. JOI plans to buy a commuter pass between station and station .
- The third line contains two integers and separated by a space. JOI wants to minimize the cost from station to station .
- The -th of the following lines () contains three integers , , separated by spaces. Railway connects station and station in both directions, and its fare is yen.
Output
Print one line to standard output containing the minimum cost from station to station when the route for the commuter pass is chosen appropriately.
Constraints
- or
- Every station is reachable from every other station by railways.
- ()
- For every , or .
- ()