标签:cstring lse getch turn stream char ios || The
/* 由于道路的互补性 最短路不会再同一时刻相交 所以实际上就是跑两个最短路 取max */ #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<iostream> #define M 410 #define ll long long using namespace std; int f[M][M], d[M][M]; const int inf = 1000000000; int read() { int nm = 0, f = 1; char c = getchar(); for(; !isdigit(c); c = getchar()) if(c == ‘-‘) f = -1; for(; isdigit(c); c = getchar()) nm = nm * 10 + c -‘0‘; return nm * f; } int main() { int n = read(), m = read(); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i == j) continue; else f[i][j] = inf; for(int i = 1; i <= m; i++) { int vi = read(), vj = read(); f[vi][vj] = f[vj][vi] = 1; } for(int i =1; i <= n; i++) for(int j = 1; j <= n; j++) { if(i == j) continue; if(f[i][j] == 1) d[i][j] = inf; else d[i][j] = 1; } 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]); d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } if(f[1][n] == inf || d[1][n] == inf) return puts("-1"); cout << max(f[1][n], d[1][n]); return 0; }
标签:cstring lse getch turn stream char ios || The
原文地址:https://www.cnblogs.com/luoyibujue/p/9240942.html