This problem has a small n, so floyd can also be competent for the shortest time;
Let g[i][j][k] represent that there is a path with weight k from i to j at present;
Since the edge weights are all 1, g[i][j][k]=g[i][p][k-1]+g[p][j][k-1];
For f[i][j]; if i to j can be passed in 1 step, then = 1; otherwise = inf;
Then just run floyd;
#include <bits/stdc++.h>
using namespace std;
int b[110][110][40];
int f[110][110];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(register int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
b[u][v][0]=1;
f[u][v]=1;
}
for(int k=1;k<=30;k++){
for(int p=1;p<=n;p++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i][p][k-1]&&b[p][j][k-1]){
b[i][j][k]=1;
f[i][j]=1;
}
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(f[i][j]!=1) f[i][j]=999999999;
}
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
}
}
cout<<f[1][n];
}
Reprinted in: https://www.cnblogs.com/kamimxr/p/11607751.html