Submission #101178

#TimeUsernameProblemLanguageResultExecution timeMemory
101178square1001007 (CEOI14_007)C++14
100 / 100
419 ms17144 KiB
#include <queue> #include <vector> #include <iostream> using namespace std; const int inf = 1012345678; vector<int> shortest_path(int src, vector<vector<int> > &G) { int N = G.size(); vector<int> dist(N, inf); queue<int> que; que.push(src); dist[src] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i : G[u]) { if (dist[i] == inf) { dist[i] = dist[u] + 1; que.push(i); } } } return dist; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; int S, D, A, B; cin >> S >> D >> A >> B; --S, --D, --A, --B; vector<vector<int> > G(N); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } vector<int> da = shortest_path(A, G); vector<int> db = shortest_path(B, G); if (da[S] != db[S] || da[D] != db[D]) { int ans = min(da[D] - da[S], db[D] - db[S]); cout << (ans < 0 ? -1 : ans) << endl; } else { vector<int> ds = shortest_path(S, G); vector<int> dd = shortest_path(D, G); int mins = inf, mind = inf; for (int i = 0; i < N; ++i) { if (da[i] == db[i]) { if (da[i] + ds[i] == da[S]) mins = min(mins, da[i]); if (da[i] + dd[i] == da[D]) mind = min(mind, da[i]); } } int ans = da[D] - da[S] - (mins > mind ? 1 : 0); cout << (ans < 0 ? -1 : ans) << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...