Submission #82432

# Submission time Handle Problem Language Result Execution time Memory
82432 2018-10-30T16:39:15 Z atoiz 007 (CEOI14_007) C++14
100 / 100
308 ms 18204 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cassert>
#include <climits>
#include <fstream>
#include <stack>
#include <queue>
#include <map>

using namespace std;

const string FILE_NAME = "spy";
const string IN_FILE = FILE_NAME + ".inp";
const string OUT_FILE = FILE_NAME + ".out";

int readInt()
{
    char ch = getchar(); int ans = 0, sgn = 1;
    while (ch == ' ' || ch == '\n') ch = getchar();
    if (ch == '-') sgn = -1, ch = getchar();
    while (47 < ch && ch < 58) ans = ans * 10 + ch - 48, ch = getchar();
    return ans * sgn;
}

const int INF = 1e8;
const int MAX = 200010;
int n, m;
int s, d, a, b;
vector< vector<int> > dist(3, vector<int>(MAX, INF));
vector<int> deci(MAX, INF);
vector<int> gr[MAX];

int32_t main()
{
//	freopen(IN_FILE.c_str(), "r", stdin);
//	freopen(OUT_FILE.c_str(), "w", stdout);

	n = readInt(); m = readInt();
	s = readInt(); d = readInt(); a = readInt(); b = readInt();
    for (int i = 0; i < m; ++i) {
		int u = readInt(), v = readInt();
        gr[u].push_back(v);
        gr[v].push_back(u);
    }


	dist[0][a] = 0;
	queue<int> qu; qu.push(a);
    while (!qu.empty()) {
        int u = qu.front(); qu.pop();
        for (int v : gr[u]) {
            if (dist[0][v] == INF) {
                dist[0][v] = dist[0][u] + 1;
                qu.push(v);
            }
        }
    }

	dist[1][b] = 0;
	assert(qu.empty());
	qu.push(b);
    while (!qu.empty()) {
        int u = qu.front(); qu.pop();
        for (int v : gr[u]) {
            if (dist[1][v] == INF) {
                dist[1][v] = dist[1][u] + 1;
                qu.push(v);
            }
        }

        deci[u] = dist[1][u];
        for (int v : gr[u]) {
            if (dist[0][v] + 1 == dist[0][u] && dist[1][v] + 1 == dist[1][u]) {
                deci[u] = min(deci[u], deci[v]);
            }
        }
    }

//    for (int i = 1; i <= n; ++i) cerr << deci[i] << endl;

	dist[2][d] = 0;
	assert(qu.empty());
	qu.push(d);
    while (!qu.empty()) {
        int u = qu.front(); qu.pop();
        for (int v : gr[u]) {
            if (dist[2][v] == INF) {
                dist[2][v] = dist[2][u] + 1;
                qu.push(v);
            }
        }
    }

	int ans = INF;

	for (int u = 1; u <= n; ++u) {
        bool block;
        if (dist[0][s] > dist[0][u]) block = 0;
        else if (dist[1][s] > dist[1][u]) block = 0;
        else if (dist[0][s] < dist[0][u]) block = 1;
        else if (dist[1][s] < dist[1][u]) block = 1;
        else if (deci[s] <= deci[u]) block = 1;
        else block = 0;

        if (u == d && (!block)) {
            cout << -1;
            return 0;
        }
        if (!block) {
            ans = min(ans, dist[2][u] - 1);
        }
	}
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8168 KB Output is correct
2 Correct 10 ms 8292 KB Output is correct
3 Correct 9 ms 8344 KB Output is correct
4 Correct 9 ms 8344 KB Output is correct
5 Correct 9 ms 8344 KB Output is correct
6 Correct 9 ms 8456 KB Output is correct
7 Correct 9 ms 8456 KB Output is correct
8 Correct 9 ms 8456 KB Output is correct
9 Correct 9 ms 8456 KB Output is correct
10 Correct 9 ms 8456 KB Output is correct
11 Correct 9 ms 8456 KB Output is correct
12 Correct 9 ms 8456 KB Output is correct
13 Correct 12 ms 8456 KB Output is correct
14 Correct 9 ms 8456 KB Output is correct
15 Correct 9 ms 8472 KB Output is correct
16 Correct 9 ms 8556 KB Output is correct
17 Correct 10 ms 8556 KB Output is correct
18 Correct 9 ms 8556 KB Output is correct
19 Correct 9 ms 8556 KB Output is correct
20 Correct 9 ms 8556 KB Output is correct
21 Correct 9 ms 8556 KB Output is correct
22 Correct 9 ms 8556 KB Output is correct
23 Correct 9 ms 8556 KB Output is correct
24 Correct 10 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 9708 KB Output is correct
2 Correct 36 ms 10220 KB Output is correct
3 Correct 30 ms 10220 KB Output is correct
4 Correct 36 ms 10348 KB Output is correct
5 Correct 44 ms 10348 KB Output is correct
6 Correct 28 ms 10348 KB Output is correct
7 Correct 30 ms 10348 KB Output is correct
8 Correct 31 ms 10348 KB Output is correct
9 Correct 63 ms 10420 KB Output is correct
10 Correct 128 ms 14572 KB Output is correct
11 Correct 54 ms 14572 KB Output is correct
12 Correct 76 ms 14572 KB Output is correct
13 Correct 66 ms 14572 KB Output is correct
14 Correct 49 ms 14572 KB Output is correct
15 Correct 70 ms 14572 KB Output is correct
16 Correct 117 ms 14572 KB Output is correct
17 Correct 86 ms 14572 KB Output is correct
18 Correct 71 ms 14572 KB Output is correct
19 Correct 120 ms 14572 KB Output is correct
20 Correct 190 ms 15832 KB Output is correct
21 Correct 105 ms 15832 KB Output is correct
22 Correct 92 ms 15832 KB Output is correct
23 Correct 108 ms 15832 KB Output is correct
24 Correct 99 ms 15832 KB Output is correct
25 Correct 94 ms 15832 KB Output is correct
26 Correct 90 ms 15832 KB Output is correct
27 Correct 116 ms 15832 KB Output is correct
28 Correct 139 ms 15832 KB Output is correct
29 Correct 143 ms 15832 KB Output is correct
30 Correct 222 ms 16244 KB Output is correct
31 Correct 121 ms 16244 KB Output is correct
32 Correct 126 ms 16244 KB Output is correct
33 Correct 112 ms 16244 KB Output is correct
34 Correct 158 ms 16244 KB Output is correct
35 Correct 124 ms 16244 KB Output is correct
36 Correct 130 ms 16244 KB Output is correct
37 Correct 127 ms 16244 KB Output is correct
38 Correct 153 ms 16244 KB Output is correct
39 Correct 178 ms 16244 KB Output is correct
40 Correct 221 ms 16244 KB Output is correct
41 Correct 308 ms 18204 KB Output is correct