Bickford Fuse
InterviewTime limit2sMemory limit1024 MB
Given a graph of knots and ropes with burn times, find the knot whose lighting lets the whole fuse burn out in the least time, and report that time.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Statement
On a sunny day, Hedgehog was walking through his favorite clearing when he noticed something unusual that had never been there before. From a distance it looked like a ball of tangled ropes. Up close, after he untangled it and examined the material, Hedgehog concluded that it was a Bickford fuse.
Hedgehog saw that whoever made it must have been bored and had a lot of free time. The fuse was knotted, and it had a huge number of knots! After counting carefully, he found that there are knots and connections between them. Each connection joins two knots and burns at a uniform rate. When a knot is lit, all connections that contain it start to burn.
Hedgehog did not like the find, so he decided to destroy it. He had a match in his pocket that he had forgotten about. He can light one of the knots with it, and the whole structure starts to burn. Hedgehog is a busy hedgehog and wants the structure to burn as fast as possible. Help him determine which knot he should light.
Input
The first line contains two integers and (, ), the number of knots and the number of ropes between them.
Each of the next lines contains three integers , , and (, , ), the numbers of the knots joined by the -th rope and the number of seconds that rope takes to burn completely when lit from one of its ends.
Each pair of knots is joined by at most one rope. It is guaranteed that the whole fuse can burn if any one of its knots is lit.
Output
Print a pair of numbers: the number of the knot to light, and the time it takes for the whole fuse to burn when lit at that knot.