atcoder#ARC159A. [ARC159A] Copy and Paste Graph
[ARC159A] Copy and Paste Graph
Score : points
Problem Statement
You are given an -by- matrix , where .
We have the following directed graph.
- The graph has vertices numbered .
- The edges correspond to the -by- matrix obtained by arranging copies of in rows and columns (see Sample Input/Output 1 for an example). If , there is a directed edge from vertex to vertex ; if , that edge does not exist.
Answer the following question for .
- Find the shortest length (number of edges) of a path from vertex to vertex . If there is no such path, print
-1
instead.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the question for .
3 2
1 1 1
1 1 0
0 1 0
4
1 2
1 4
1 6
6 3
1
1
1
3
Below is the matrix representing the edges.
1 1 1 1 1 1
1 1 0 1 1 0
0 1 0 0 1 0
1 1 1 1 1 1
1 1 0 1 1 0
0 1 0 0 1 0
4 1000000000
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
1
1 4000000000
-1
There is no edge.