Codeforces 601A
Posted NWU_ACM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 601A相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h> using namespace std; #define maxn 411 #define INF 11111 int a1[maxn][maxn], a2[maxn][maxn]; int n, m; int d[maxn]; bool vis[maxn]; int dij (int a[maxn][maxn]) { for (int i = 1; i <= n; i++) { d[i] = INF; vis[i] = 0; } d[1] = 0; for (int j = 1; j <= n; j++) { int k = -1; int Min = INF; for (int i = 1; i <= n; i++) { if (!vis[i] && d[i] < Min) { Min = d[i]; k = i; } } if (k == -1) break; vis[k] = 1; for (int i = 1; i <= n; i++) { if (!vis[i]) d[i] = min (d[i], d[k]+a[k][i]); } } return d[n]; } int main () { scanf ("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a1[i][j] = a2[i][j] = INF; } } int u, v; for (int i = 1; i <= m; i++) { scanf ("%d%d", &u, &v); a1[u][v] = a1[v][u] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; if (a1[i][j] == INF) a2[i][j] = 1; } } int ans1 = dij (a1); int ans2 = dij (a2); if (ans1 >= INF || ans2 >= INF) cout << "-1" << endl; else cout << max (ans1, ans2) << endl; return 0; }
以上是关于Codeforces 601A的主要内容,如果未能解决你的问题,请参考以下文章
The Two Routes CodeForces - 601A(水最短路)
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段