Don't Copy the Manga
Time limit1sMemory limit1024 MB
Given a weighted multigraph, find the tree on Hanbyeol's route from B to C that Toka can reach fastest from A, breaking ties by smallest number.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, BFS
- Solved
- No attempts yet
Problem
Toka fell for the beautiful Hanbyeol senior she happened to run into! She wanted to talk to him but did not have the nerve, so she started looking for an excuse to speak to him. Toka reads a lot of manga, and in one comic she found a way to make that excuse. In the story, the male and female leads became friends after bumping into each other under cherry blossoms on the way to school. Toka decided to copy it.
Toka's neighborhood has no cherry trees, so she plans to try it at a place with ginkgo trees instead. Her neighborhood has ginkgo trees numbered from to , and roads with a ginkgo tree at each end. Every road can be traveled in both directions, and any two ginkgo trees can always be reached from each other. Two different ginkgo trees may be connected by more than one road.
Hanbyeol likes open places. When he moves from one ginkgo tree to an adjacent one, he goes to the tree with the most roads connected to it. If several trees tie, he goes to the one with the largest number. Following only roads with many connections could make him circle the same roads forever, so he takes the route that passes through the fewest ginkgo trees on the way to school. Even if a tree has many roads, he does not head there if he cannot reduce the number of trees he passes through.
Toka wants to spend more time with Hanbyeol, so she plans to bump into him at the ginkgo tree on his route to school that she can reach from her house the fastest. If several trees tie, she will bump into him at the one with the smallest number. Since Toka is getting ahead of herself, you need to tell her the number of the ginkgo tree where she and Hanbyeol are fated to collide.
Input
The first line contains the number of ginkgo trees () and the number of roads (), separated by a space.
The second line contains , the number of the ginkgo tree next to Toka's house, , the number of the ginkgo tree next to Hanbyeol's house, and , the number of the ginkgo tree next to the school, separated by spaces. (; ; ; )
From the third line onward, lines follow. Each line contains the numbers and of the ginkgo trees at the two ends of a road (; ) and the time it takes to travel along the road (), separated by spaces.
Output
Print the number of the ginkgo tree where Toka must go to meet her fate.