[2019 CSP-S before training camp] [Luo Gu P1613] on foot

Topic links: https://www.luogu.org/problem/P1613

Topic has prompted very obvious!

You can run each 2 K km obvious thing about it and the multiplication;

Look data range, n <= 50, which is to seek prompt us to use floyd shortest ah;

Accordingly solution came out, an array of open (eg: C [i] [j] [k]),

Represents from point i to point j satisfying whether there is a path of length 2 K , pitted again floyd're done.

(Note that the longest path maxlongint, in fact longint is int, do not mess the range of k is 64, 32 is sufficient)

 1 #include <bits/stdc++.h>
 2 #define MAXN 50+5
 3 using namespace std;
 4 int n,m,ans;
 5 int C[MAXN][MAXN][MAXN],c[MAXN][MAXN];
 6 void floyd()
 7 {
 8     for(int k=1;k<=n;k++)
 9     {
10         for(int i=1;i<=n;i++)
11         {
12             for(int j=1;j<=n;j++)
13             {
14                 if(c[i][j]>c[i][k]+c[k][j])
15                     c[i][j]=c[i][k]+c[k][j];
16             }
17         }
18     }
19 }
20 int main()
21 {
22     scanf("%d%d",&n,&m);
23     memset(c,0x3f,sizeof(c));
24     for(int i=1;i<=m;i++)
25     {
26         int u,v;
27         scanf("%d%d",&u,&v);
28         C[u][v][0]=1;
29         c[u][v]=1;
30     }
31     for(int k=1;k<=32;k++)
32     {
33         for(int i=1;i<=n;i++)
34             for(int j=1;j<=n;j++)
35                 for(int p=1;p<=n;p++)
36                     if(C[i][j][k-1]&&C[j][p][k-1])
37                     {
38                         C[i][p][k]=1;
39                         c[i][p]=1;
40                     }
41     }
42     floyd();
43     printf("%d",c[1][n]);
44     return 0;
45 }

 

Guess you like

Origin www.cnblogs.com/Xx-queue/p/11701125.html