跑路

cun[i][j][t]表示从i到j一条长度为2^t的路径是否存在

要回构造

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 const int maxn=67;
 8 const int INF=0x3f;
 9 int n,m;
10 int dis[maxn][maxn];
11 bool cun[maxn][maxn][maxn];
12 int main(){
13   memset(dis,INF,sizeof(dis));
14   cin>>n>>m;
15   for(int i=1;i<=m;i++){
16     int u,v;cin>>u>>v;
17     cun[u][v][0]=1;
18     dis[u][v]=1;
19   }
20   for(int t=1;t<=64;t++){
21     for(int i=1;i<=n;i++){
22       for(int k=1;k<=n;k++){
23         for(int j=1;j<=n;j++){
24           if(cun[i][k][t-1]&&cun[k][j][t-1]){
25             cun[i][j][t]=true;dis[i][j]=1;
26           }
27         }
28       }
29     }
30   }
31   for(int k=1;k<=n;k++)
32     for(int i=1;i<=n;i++)
33       for(int j=1;j<=n;j++)
34         dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
35   cout<<dis[1][n]<<endl;
36 } 
原文地址:https://www.cnblogs.com/lcan/p/9897909.html