USACO 2021 January Contest, Gold Problem 2 - Telephone
Problem Link
Time Complexity: O(Nlog(KlogK))
Algorithm used: Dijkstra's algorithm, Binary Search
This is where I understood how to solve this problem in time.
In this problem, the breeds of N cows are given, followed by a K by K matrix, describing whether or not a cow of breed i is willing to transmit a message to cow of breed j. The cost of sending a message from cow i to cow j is |i - j|. Find the minimum possible cost of transmitting a message from cow 1 to cow N.
This problem can be represented as a graph problem, where the cows are the nodes and edges are drawn between the nodes depending on the compatibility of cow breeds, described in the matrix. While reading in the matrix, also create an adjacency list for every breed.
Intuitively, we could run a standard Dijkstra's algorithm starting from cow 1 and loop over the adjacency list for every pass of the algorithm. Although this implementation would always produce the correct answer, the program would exceed time limit, since there will be N² edges to examine for every pass of the algorithm (time complexity: O(Nlog(N²)). To solve this problem, we just need to examine fewer edges.
When node a is being processed, choose the two adjacent nodes to a from each breed's adjacency list and reduce these nodes' distances. For example, let's say that breed 1 has cows 2, 5, 6 and node a = 3. Here, the two adjacent nodes are 2 and 5. Nodes 2 and 5's distances will get reduced according to standard Dijkstra's algorithm's logic and then added to the queue to be processed. This way, we only examine at most 2K edges for every node being processed. We examine the two adjacent nodes to a because those are the two possible ways a message can be transmitted (forwards and backwards). Examining any other nodes would be redundant since that would only increase the total cost.
I use binary search to find the two adjacent nodes of node a in each breed's adjacency list.
There is one exception to this modified Dijkstra's algorithm: if node 1 and node N are part of the same breed, then the answer may come out to be -1. If node N's breed is not compatible with it's own breed, then only the smallest node in node N's breed's adjacency list will get visited. To fix this, check: if the current node being examined has the same breed as cow N, then answer could be i - 1 + |i - N|, as the cost to travel from cow 1 to cow i is i - 1 and the cost to travel from cow i to cow N is |i - N|.
Comments