답안 #92921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92921 2019-01-05T16:36:07 Z Flying_dragon_02 007 (CEOI14_007) C++14
100 / 100
325 ms 17248 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int N = 2e5 + 5;

int n, m, s, d, a, b, dista[N], distb[N], dist[N];

vector<int> graph[N];

queue<int> q;

int get(int f) {
    int res = 0;
    for(int i = 1; i <= N - 5; i++)
        dist[i] = N;
    dist[f] = 0;
    q.push(f);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        res = max(res, dist[u]);
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(dist[v] > dist[u] + 1 && dista[v] + 1 == dista[u] && distb[v] + 1 == distb[u]) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
    return res;
}

int main() {
    //freopen("test.inp", "r", stdin);
    scanf("%d%d", &n, &m);
    scanf("%d%d%d%d", &s, &d, &a, &b);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        graph[u].pb(v);
        graph[v].pb(u);
    }
    for(int i = 1; i <= N - 5; i++) {
        dista[i] = N;
        distb[i] = N;
    }
    dista[a] = 0;
    q.push(a);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(dista[v] > dista[u] + 1) {
                dista[v] = dista[u] + 1;
                q.push(v);
            }
        }
    }
    distb[b] = 0;
    q.push(b);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(distb[v] > distb[u] + 1) {
                distb[v] = distb[u] + 1;
                q.push(v);
            }
        }
    }
    int w1 = dista[d] - dista[s], w2 = distb[d] - distb[s];
    if(min(w1, w2) < 0) {
        printf("-1");
        exit(0);
    }
    int ans;
    if(w1 != w2) {
        printf("%d", min(w1, w2));
        exit(0);
    }
    else {
        int lmaoa = get(s), lmaob = get(d);
        if(lmaoa + w1 >= lmaob)
            ans = w1;
        else
            ans = w1 - 1;
    }
    if(ans < 0)
        printf("-1");
    else
        printf("%d", ans);
}

Compilation message

007.cpp: In function 'int get(int)':
007.cpp:30:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
007.cpp: In function 'int main()':
007.cpp:60:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
007.cpp:73:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
007.cpp:43: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:44: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:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6648 KB Output is correct
2 Correct 6 ms 7416 KB Output is correct
3 Correct 6 ms 7416 KB Output is correct
4 Correct 6 ms 7416 KB Output is correct
5 Correct 6 ms 7416 KB Output is correct
6 Correct 7 ms 7416 KB Output is correct
7 Correct 6 ms 7416 KB Output is correct
8 Correct 7 ms 7416 KB Output is correct
9 Correct 7 ms 7388 KB Output is correct
10 Correct 6 ms 6520 KB Output is correct
11 Correct 6 ms 6520 KB Output is correct
12 Correct 7 ms 7416 KB Output is correct
13 Correct 7 ms 7416 KB Output is correct
14 Correct 7 ms 7360 KB Output is correct
15 Correct 6 ms 7416 KB Output is correct
16 Correct 7 ms 7388 KB Output is correct
17 Correct 7 ms 7416 KB Output is correct
18 Correct 7 ms 7416 KB Output is correct
19 Correct 7 ms 7416 KB Output is correct
20 Correct 7 ms 7416 KB Output is correct
21 Correct 6 ms 6648 KB Output is correct
22 Correct 7 ms 7416 KB Output is correct
23 Correct 8 ms 7416 KB Output is correct
24 Correct 9 ms 7416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 8568 KB Output is correct
2 Correct 31 ms 9240 KB Output is correct
3 Correct 26 ms 8696 KB Output is correct
4 Correct 35 ms 9436 KB Output is correct
5 Correct 25 ms 7800 KB Output is correct
6 Correct 24 ms 7928 KB Output is correct
7 Correct 27 ms 8924 KB Output is correct
8 Correct 28 ms 8952 KB Output is correct
9 Correct 43 ms 9332 KB Output is correct
10 Correct 156 ms 13560 KB Output is correct
11 Correct 49 ms 10204 KB Output is correct
12 Correct 63 ms 10872 KB Output is correct
13 Correct 54 ms 10456 KB Output is correct
14 Correct 44 ms 9208 KB Output is correct
15 Correct 62 ms 11000 KB Output is correct
16 Correct 73 ms 10452 KB Output is correct
17 Correct 62 ms 10744 KB Output is correct
18 Correct 74 ms 10868 KB Output is correct
19 Correct 112 ms 11900 KB Output is correct
20 Correct 209 ms 14868 KB Output is correct
21 Correct 81 ms 12408 KB Output is correct
22 Correct 96 ms 11688 KB Output is correct
23 Correct 122 ms 12368 KB Output is correct
24 Correct 108 ms 12280 KB Output is correct
25 Correct 92 ms 12024 KB Output is correct
26 Correct 79 ms 11768 KB Output is correct
27 Correct 95 ms 12408 KB Output is correct
28 Correct 115 ms 12500 KB Output is correct
29 Correct 176 ms 13176 KB Output is correct
30 Correct 231 ms 15224 KB Output is correct
31 Correct 125 ms 13236 KB Output is correct
32 Correct 121 ms 12252 KB Output is correct
33 Correct 115 ms 12536 KB Output is correct
34 Correct 135 ms 12764 KB Output is correct
35 Correct 105 ms 12536 KB Output is correct
36 Correct 118 ms 12760 KB Output is correct
37 Correct 139 ms 12756 KB Output is correct
38 Correct 109 ms 13304 KB Output is correct
39 Correct 170 ms 13432 KB Output is correct
40 Correct 211 ms 15036 KB Output is correct
41 Correct 325 ms 17248 KB Output is correct