#P7297. [USACO21JAN] Telephone G
[USACO21JAN] Telephone G
Description
Farmer John has cows, numbered , standing in a line (). The breed ID of cow is , in the range , where . The cows need your help to find the best way to transmit a message from cow to cow .
Transmitting a message from cow to cow takes time . However, not all breeds of cows are willing to talk to each other, as described by a matrix . If a cow of breed is willing to transmit a message to a cow of breed , then , otherwise it is . It is not guaranteed that , and it is possible that if cows of breed are not willing to communicate with each other.
Find the minimum time required to transmit the message.
Input Format
The first line contains and .
The next line contains space-separated integers .
The following lines describe the matrix . Each line contains a string of binary digits. For the -th string from top to bottom, its -th character is .
Output Format
Output one integer, the minimum time required. If it is impossible to transmit the message from cow to cow , output .
5 4
1 4 2 3 4
1010
0001
0110
0100
6
Hint
The optimal transmission sequence is . The total time is .
Test Point Properties:
- Test points 1-5 satisfy .
- Test points 6-13 have no additional constraints.
Problem provided by: Dhruv Rohatgi
Translated by ChatGPT 5
京公网安备 11011102002149号