0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 13:58:48 +00:00

P1613 跑路

R54032506
This commit is contained in:
Baoshuo Ren 2021-07-23 22:34:00 +08:00 committed by Baoshuo Ren
parent 951177b171
commit 4bbb813cfe
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, g[55][55];
bool f[55][55][55];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = i == j ? 0 : 0x3f3f3f3f;
}
}
for (int i = 1; i <= m; i++) {
cin >> u >> v;
g[u][v] = f[u][v][0] = 1;
}
for (int p = 1; p <= 32; p++) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (f[i][k][p - 1] && f[k][j][p - 1]) {
f[i][j][p] = true;
g[i][j] = 1;
}
}
}
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
cout << g[1][n] << endl;
return 0;
}