The Admiral
Time limit1sMemory limit128 MB
Find two vertex- and edge-disjoint (except at endpoints) directed paths from node 1 to node v in a weighted graph minimizing total edge weight, which requires a min-cost flow formulation with vertex splitting.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Greedy
- Solved
- No attempts yet
Statement
Michiel de Ruyter is the most famous admiral in the history of the Netherlands. He distinguished himself in the Anglo-Dutch Wars of the 17th century.
Graph theory began to be studied during de Ruyter's lifetime, and the admiral often applied it to his naval battle plans. Each intermediate point at sea is represented as a vertex, and every sea route leading from one point to another is a directed edge. Between two points and there is at most one route . The weight of each edge is the number of cannonballs that must be fired to pass along that route safely.
De Ruyter's most famous tactic is the "De Ruyter Manoeuvre". In it, two warships leave a single point heading in different directions. Each ship moves while fighting enemy vessels, and the two reunite at the destination. The two ships must always choose non-overlapping routes: apart from the start and the destination, they may not pass through the same intermediate point or use the same route.
De Ruyter dislikes wasting money, so he wants to choose the two ships' routes so that the total number of cannonballs fired is as small as possible.
Input
The input consists of several test cases. The end of the input is marked by end-of-file (EOF).
The first line of each test case contains the number of intermediate points and the number of routes (, ). Each of the next lines contains the description of a route , , (, , ). Here is the starting point of the route, is its ending point, and is the number of cannonballs that must be fired to travel along it.
The tactic starts at point and ends at point . There are always at least two non-overlapping paths between points and .
Output
For each test case, print on its own line the minimum total number of cannonballs the two warships must fire while following the tactic.
Note
In the first test case the two ships (red and blue) start at point and meet at point . The red ship travels ( cannonballs) and the blue ship travels ( cannonballs). Apart from the start and the end, the two paths share no vertex or edge, and the total is cannonballs.