# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30121 | cdemirer | 007 (CEOI14_007) | C++14 | 1000 ms | 25796 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llp;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
int N, M;
int Z, D, SV1, SV2;
vvi edges;
int sv1dists[200000], sv2dists[200000];
void dijsktra(int *dists, int s) {
set<ii> S;
for (int i = 0; i < N; i++) {
if (i == s) {
dists[i] = 0;
S.insert(mp(dists[i], i));
continue;
}
dists[i] = (int)1e9;
S.insert(mp(dists[i], i));
}
while ( ! S.empty() ) {
ii x = *S.begin();
S.erase(S.begin());
for (int i = 0; i < edges[x.second].size(); i++) {
if (dists[x.second] + 1 < dists[edges[x.second][i]]) {
S.erase(mp(dists[edges[x.second][i]], edges[x.second][i]));
dists[edges[x.second][i]] = dists[x.second] + 1;
S.insert(mp(dists[edges[x.second][i]], edges[x.second][i]));
}
}
}
}
int moveit(int x) {
int cnt = 0;
while (true) {
bool fnd = false;
for (int i = 0; i < edges[x].size(); i++) {
int v = edges[x][i];
if (sv1dists[v] == sv1dists[x]-1 && sv1dists[v] == sv2dists[v]) {
fnd = true;
x = v;
break;
}
}
if (!fnd) return cnt;
else cnt++;
}
}
int main(int argc, char **argv) {
//freopen("input", "r", stdin);
//freopen("output", "w", stdout);
scanf("%d%d", &N, &M);
scanf("%d%d%d%d", &Z, &D, &SV1, &SV2);
Z--; D--; SV1--; SV2--;
edges.resize(N);
for (int i = 0; i < M; i++) {
int a, b; scanf("%d%d", &a, &b);
a--, b--;
edges[a].pb(b);
edges[b].pb(a);
}
dijsktra(sv1dists, SV1);
dijsktra(sv2dists, SV2);
int ans = -1;
ans = max(ans, min(sv1dists[D] - sv1dists[Z], sv2dists[D] - sv2dists[Z]));
if (ans != -1 && sv1dists[D] == sv2dists[D] && sv1dists[Z] == sv2dists[Z]) {
int a = moveit(D);
int b = moveit(Z);
if (a > b + ans) ans--;
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |