Submission #155934

# Submission time Handle Problem Language Result Execution time Memory
155934 2019-10-02T05:44:05 Z Akashi 007 (CEOI14_007) C++14
100 / 100
374 ms 17892 KB
#include <bits/stdc++.h>
using namespace std;

int n, m;
bool viz[200005];
int s, d, a, b;
int da[200005], db[200005], ds[200005], dd[200005];
queue <int> q;
vector <int> v[200005];

void bfs(int nod, int d[]){
    for(int i = 1; i <= n ; ++i) d[i] = 1e9;
    memset(viz, 0, sizeof(viz));

    q.push(nod);
    d[nod] = 0; viz[nod] = 1;
    while(!q.empty()){
        int nod = q.front();
        q.pop();

        for(auto it : v[nod]){
            if(viz[it]) continue ;
            q.push(it);
            viz[it] = 1;
            d[it] = d[nod] + 1;
        }
    }
}

int main()
{
//    freopen("1.in", "r", stdin);
    scanf("%d%d", &n, &m);
    scanf("%d%d%d%d", &s, &d, &a, &b);

    int x, y;
    for(int i = 1; i <= m ; ++i){
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }

    bfs(a, da);
    bfs(b, db);
    bfs(s, ds);
    bfs(d, dd);

    if(dd[b] < ds[b] || dd[a] < ds[a]){
        printf("-1");
        return 0;
    }

    int tmp = -1;
    if(dd[b] - ds[b] != dd[a] - ds[a])
        tmp = min(dd[b] - ds[b], dd[a] - ds[a]);
    else{
        int dif = min(dd[b] - ds[b], dd[a] - ds[a]), nr1 = 0, nr2 = 0;
        for(int i = 1; i <= n ; ++i)
            if(da[i] == db[i] && da[i] + ds[i] == ds[a]) nr1 = max(nr1, ds[i]);

        for(int i = 1; i <= n ; ++i)
            if(da[i] == db[i] && da[i] + dd[i] == dd[a]) nr2 = max(nr2, dd[i]);

        if(nr1 >= nr2 - dif) tmp = min(dd[b] - ds[b], dd[a] - ds[a]);
        else tmp = min(dd[b] - ds[b], dd[a] - ds[a]) - 1;
    }

    printf("%d", tmp);

    return 0;
}





















Compilation message

007.cpp: In function 'int main()':
007.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
007.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d", &s, &d, &a, &b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5240 KB Output is correct
2 Correct 6 ms 5152 KB Output is correct
3 Correct 6 ms 5240 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 6 ms 5240 KB Output is correct
6 Correct 6 ms 5240 KB Output is correct
7 Correct 6 ms 5240 KB Output is correct
8 Correct 6 ms 5240 KB Output is correct
9 Correct 6 ms 5240 KB Output is correct
10 Correct 6 ms 5188 KB Output is correct
11 Correct 6 ms 5212 KB Output is correct
12 Correct 6 ms 5240 KB Output is correct
13 Correct 6 ms 5240 KB Output is correct
14 Correct 6 ms 5240 KB Output is correct
15 Correct 6 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 7 ms 5240 KB Output is correct
21 Correct 7 ms 5240 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 6 ms 5240 KB Output is correct
24 Correct 7 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7132 KB Output is correct
2 Correct 49 ms 8056 KB Output is correct
3 Correct 40 ms 7416 KB Output is correct
4 Correct 52 ms 8184 KB Output is correct
5 Correct 35 ms 7032 KB Output is correct
6 Correct 37 ms 7288 KB Output is correct
7 Correct 45 ms 7676 KB Output is correct
8 Correct 48 ms 7516 KB Output is correct
9 Correct 60 ms 7928 KB Output is correct
10 Correct 187 ms 12280 KB Output is correct
11 Correct 86 ms 9592 KB Output is correct
12 Correct 110 ms 10488 KB Output is correct
13 Correct 92 ms 9848 KB Output is correct
14 Correct 74 ms 9080 KB Output is correct
15 Correct 106 ms 10616 KB Output is correct
16 Correct 126 ms 10972 KB Output is correct
17 Correct 94 ms 10360 KB Output is correct
18 Correct 120 ms 10360 KB Output is correct
19 Correct 153 ms 11640 KB Output is correct
20 Correct 283 ms 14416 KB Output is correct
21 Correct 170 ms 12832 KB Output is correct
22 Correct 149 ms 11888 KB Output is correct
23 Correct 154 ms 12624 KB Output is correct
24 Correct 165 ms 12616 KB Output is correct
25 Correct 141 ms 12284 KB Output is correct
26 Correct 141 ms 11800 KB Output is correct
27 Correct 160 ms 12716 KB Output is correct
28 Correct 203 ms 12740 KB Output is correct
29 Correct 174 ms 13308 KB Output is correct
30 Correct 264 ms 15124 KB Output is correct
31 Correct 210 ms 13816 KB Output is correct
32 Correct 193 ms 12796 KB Output is correct
33 Correct 165 ms 12920 KB Output is correct
34 Correct 238 ms 13292 KB Output is correct
35 Correct 187 ms 13176 KB Output is correct
36 Correct 202 ms 13472 KB Output is correct
37 Correct 254 ms 14328 KB Output is correct
38 Correct 243 ms 14200 KB Output is correct
39 Correct 295 ms 14268 KB Output is correct
40 Correct 306 ms 15708 KB Output is correct
41 Correct 374 ms 17892 KB Output is correct