Poisonous Labyrinth
Time limit3sMemory limit2048 MB
Given a weighted tree with m pairs of poison vials, find the start vertex that minimizes the distance needed to cure all poisons and return to it.
Problem
BThero needs to escape from a labyrinth shaped like a tree with vertices and edges. Each edge has a length. The vertices hold poison vials in total: two vials of each of the poison types.
When BThero enters a vertex for the first time, he drinks all the vials in that vertex immediately. If he enters a vertex he has already visited, there are no vials left to drink there.
Drinking a vial of a poison type he has not drunk before poisons him. To cure it, he must find and drink the other vial of the same type.
BThero starts at vertex and drinks the vials there at once. He then keeps moving through vertices until he is no longer poisoned. After that, he returns to vertex and leaves the labyrinth.
Find the starting vertex that minimizes the total distance BThero travels along an optimal route.
Input
The first line contains two integers and (, ), the number of vertices and the number of poison types.
Each of the next lines contains three integers , , and (, ). This describes a bidirectional edge between vertices and with length .
Each of the next lines contains two integers and (), the two vertices where the vials of poison type are located. It is possible that . In that case, BThero is poisoned as soon as he enters the vertex and is cured immediately.
Output
Print one integer on a line: the minimum distance BThero must travel to cure all poisons when he starts from the optimal vertex.