## Editorial for TLE '16 Contest 7 P5 - Shortest Path Faster Algorithm

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: d

Important: Make sure that 64-bit integers are used to store the edge weights and store in all subtasks, or else you may get 0 points.

For subtask 1, the only possible paths are and . The shorter path is preferred. Find the number of people who can use the first path and the second path. Afterwards, it is possible to solve each query in constant time.

Note: There are test cases where 0 people can travel either of these paths, and Fail is the answer for every query.

Time Complexity:

For subtask 2, perform a BFS search to get a valid path, then subtract the road durabilities by 1. Do this 300 times in total, and create a lookup table. Every query can be solved by using the lookup table.

Time Complexity:

For subtask 3, perform a BFS search to get a valid path. Once a valid path is found, find the lowest road durability in the path, and subtract the road durabilities by this value. A lookup table can also be generated. This time, every query must binary search / lower_bound to find the correct index.

The number of possible paths is (since a path removes at minimum, edge), and every BFS search is . As a result, a naive implementation may fail subtasks 2 and 4. After doing constant optimization, this approach will perform approximately million operations and can pass subtask 4. This was not the intended solution to the problem.

Note: In cases where no path exists between and , the lookup table might be empty. In this case, every query should be answered with Fail.

Well-versed programmers may notice similarities with the Edmonds–Karp algorithm.

Time Complexity:

For subtask 4, the first observation is that the path length must be monotonically increasing. It is enough to find paths with nodes. For each possible path length, perform a BFS search and categorize nodes according to their distance from vertex . If a node with distance goes to a node with distance , this edge must be included in a new graph. After all operations, this step is .

After the new graph is generated, make sure to sort outgoing edges in the new graph. Note that stacks can be used to store the graph. After all operations, this step is .

Now, paths can easily be computed from the new graph. DFS is sufficient to grab a path. Make sure to get rid of edges that lead to nowhere. Notice that the runtime of a DFS is . After a new path is found, find the smallest edge durability and reduce all edges in this path. At least 1 edge will be deleted. To disconnect nodes and , operations go to DFS, and operations go to reducing edge durabilities.

Since there are paths in total, the total number of operations is to get and store all the paths in a lookup table. Every query can be solved by binary searching the lookup table.

Well-versed programmers may notice similarities with Dinic's algorithm.

Time Complexity: