Editorial for COCI '22 Contest 3 #3 Baltazar
Submitting an official solution before solving the problem yourself is a bannable offence.
For the first subtask, we can use Dijkstra to calculate the distance between cities and , and after that for each road we can calculate the distance between cities and if the length of that road is increased by km and see if that distance is higher than the initial distance by km, for a total time complexity of .
For the second subtask, the guarantee of that subtask tells us that, no matter which road's length is increased by km, due to the road between and the length of the shortest path will be increased by at most km, so we're looking for roads whose increase of length by km changes the length of the shortest path between and . So, we're looking for roads which are on all shortest paths between and . If is the length of the shortest path between and , then let's make a directed graph in which we put an edge between and if , then each path between and in the new graph corresponds to a shortest path between and , so we need to find the edges that are in all paths from to . That can be done by comparing the number of paths from to with the number of paths from to that pass through that edge. We can use dynamic programming to calculate the number of paths from to each vertex, as well as the number of paths from each vertex to , then the number of paths between and that pass through an edge between and is equal to the product of the number of paths from to and the number of paths from to . Those numbers are very large, but we can look at them modulo randomly generated prime numbers between and . Let's calculate the probability that this method says that the total number of paths and the number of paths passing through a given edge is equal, even though in reality it isn't. For that to happen, the difference between those numbers must be divisible by all prime numbers we chose. It can easily be seen that the number of paths between and is , so their difference is , so for it has less than different prime factors. There are more than prime numbers up to . So, the chance that it's divisible by all primes is at most , and the probability that one of the edges gives a wrong answer is that multiplied by , so about . In practice, the probability is much smaller because it's impossible to make test cases such that the difference in the number of paths are whichever numbers we want; the number of ways to choose numbers is about , and the number of possible graphs is about , which is much smaller; in practice just prime number is enough.
For the third subtask, we also need to check for each road whether it's on all paths from to of length . To do that, we can check if it's on all shortest paths from to with a length of a different parity than and make sure that that shortest path has a length of . To do that, we can replace each vertex and edge with vertices and edges; the new vertices will be one for paths of odd length and one for paths of even length, and we can then do almost the same thing as for the second subtask. Then a road increases the length of the shortest path by if and only if the length of the shortest path between and with a length of different parity than is , if it's on all shortest paths between and and it's not on all shortest paths between and with a length different parity than .
Comments