原题的输出格式里有一句话:
If no such path exists from pasture_1 to pasture_i+1, output -1 alone on the line.
也就是说,只要 111 到点 i+1i+1i+1 没有路径,则第 iii 行要输出 −1-1−1。这一点翻译中没有体现。