# Telephone USACO 2021 January Contest

Page Contents

## Telephone USACO Solution

Farmer John’s NN cows, conveniently numbered 1…N1…N, are standing in a line (1≤N≤5⋅1041≤N≤5⋅104). The iith cow has a breed identifier bibi in the range 1…K1…K, with 1≤K≤501≤K≤50. The cows need your help to figure out how to best transmit a message from cow 11 to cow NN.

It takes |i−j||i−j| time to transmit a message from cow ii to cow jj. However, not all breeds are willing to communicate with each other, as described by a K×KK×K matrix SS, where Sij=1Sij=1 if a cow of breed ii is willing to transmit a message to a cow of breed jj, and 00 otherwise. It is not necessarily true that Sij=SjiSij=Sji, and it may even be the case that Sii=0Sii=0 if cows of breed ii are unwilling to communicate with each-other.

Please determine the minimum amount of time needed to transmit the message.

#### INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN and KK.

The next line contains NN space-separated integers b1,b2,…,bNb1,b2,…,bN.

The next KK lines describe the matrix SS. Each consists of a string of KK bits, SijSij being the jjth bit of the iith string from the top.

#### OUTPUT FORMAT (print output to the terminal / stdout):

Print a single integer giving the minimum amount of time needed. If it is impossible to transmit the message from cow 11 to cow NN, then output −1−1.

```5 4
1 4 2 3 4
1010
0001
0110
0100
```

#### SAMPLE OUTPUT:

```6
```

The optimal sequence of transmissions is 1→4→3→51→4→3→5. The total amount of time is |1−4|+|4−3|+|3−5|=6|1−4|+|4−3|+|3−5|=6.

#### SCORING:

• Test cases 1-5 satisfy N≤1000N≤1000.
• Test cases 6-13 satisfy no additional constraints.

Problem credits: Dhruv Rohatgi

## Solution

There’s a fairly simple quadratic-time solution: let every cow be the vertex of a graph, and say that there is a directed edge of weight |i−j||i−j| from cow ii to cow jj if cow ii is willing to talk to cow jj. Then the cost of the shortest path from cow 11 to cow NN is precisely the answer, and we can find it with e.g. Dijkstra’s algorithm. However, since the graph has O(N2)O(N2) edges, this is too slow.

That approach uses none of the structure of the original problem: that the edge weights are linear, or that the number of types of cows is very small. Let’s try to use this structure to make a different graph with fewer edges that has the same shortest-path costs.

The physical intuition for a cost of |i−j||i−j| to transmit a message from cow ii to jj is that the message travels 11 cow per unit time. Leveraging this intuition, let’s make a graph where each vertex encodes the location of the message. Then in one timestep, the message can either move left by one, or right by one. So we no longer have a quadratic number of edges.

Of course, the location of the message is not quite enough information to determine where it could go next. If we knew the cow who most recently sent the message, that would be enough information: in one unit time, a message sent by cow ii can either move one unit away from cow ii; or it can be “received” by the cow at its current location if cow ii is happy to talk to this cow. The former edge updates the message’s location, and the latter edge updates the message’s sender. Thus, this graph has O(N2)O(N2) vertices (for every location/sender pair) and O(N2)O(N2) edges (22 per vertex).

Despite appearances, that is progress: we used the specifically chosen edge weights to lower the degree of the graph to O(1)O(1). It remains to decrease the number of vertices, using the other symmetry of the problem: the small number of cow breeds. The key is that we don’t need to remember the sender, just the sender’s breed. Now we only have O(NK)O(NK) vertices and O(NK)O(NK) edges. Every edge has weight either 00 or 11 (cost 11 to move left or right; cost 00 to be received by the cow at the current location, if the breed matches the sending breed). The shortest path from (cow 11, breed of cow 11) to (cow NN, breed of cow NN) in this graph is (almost) the answer we need, and can be found by 0-1 BFS or Dijkstra’s algorithm.

There is one more catch: if the first and last cows are the same breed, this graph will always output N−1N−1 as the shortest path, which may be incorrect if this breed doesn’t talk to itself. There are a number of ways to resolve this, e.g. by changing the breed of cow NN to a fake breed 00, and remembering which breeds of cows are willing to talk to cow NN.

Here is Danny Mittal’s code:

```import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;

public class TelephoneCorrect {

public static void main(String[] args) throws IOException {
StringTokenizer tokenizer = new StringTokenizer(in.readLine());
int n = Integer.parseInt(tokenizer.nextToken());
int k = Integer.parseInt(tokenizer.nextToken());
int[] breeds = new int[n + 1];
tokenizer = new StringTokenizer(in.readLine());
for (int j = 1; j <= n; j++) {
breeds[j] = Integer.parseInt(tokenizer.nextToken());
}
boolean[][] adj = new boolean[k + 1][k + 1];
for (int b = 1; b <= k; b++) {
String line = " " + in.readLine();
for (int c = 1; c <= k; c++) {
adj[b][c] = line.charAt(c) == '1';
}
}
breeds[n] = 0;
int[][] dist = new int[k + 1][n + 1];
for (int b = 0; b <= k; b++) {
Arrays.fill(dist[b], -1);
}
dist[breeds] = 0;
while (!q.isEmpty()) {
int b = q.remove();
int j = q.remove();
if (j > 1 && dist[b][j - 1] == -1) {
dist[b][j - 1] = dist[b][j] + 1;
}
if (j < n && dist[b][j + 1] == -1) {
dist[b][j + 1] = dist[b][j] + 1;
}
if (adj[b][breeds[j]] && dist[breeds[j]][j] == -1) {
dist[breeds[j]][j] = dist[b][j];