Submission #101421

# Submission time Handle Problem Language Result Execution time Memory
101421 2019-03-19T01:33:17 Z rocketninja7 007 (CEOI14_007) C++14
100 / 100
499 ms 17768 KB
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
    int N, M, s, d, a, b;
    scanf("%d%d%d%d%d%d", &N, &M, &s, &d, &a, &b);
    vector<int> adjList[N+1];
    for(int i=0;i<M;i++){
        int u, v;
        scanf("%d%d", &u, &v);
        adjList[u].push_back(v);
        adjList[v].push_back(u);
    }
    queue<int> proc;
    proc.push(s);
    int dists[N+1];
    for(int i=1;i<N+1;i++){
        dists[i]=-1;
    }
    dists[s]=0;
    while(!proc.empty()){
        int node=proc.front();
        proc.pop();
        for(int i=0;i<adjList[node].size();i++){
            if(dists[adjList[node][i]]==-1){
                dists[adjList[node][i]]=dists[node]+1;
                proc.push(adjList[node][i]);
            }
        }
    }
    proc.push(d);
    int distd[N+1];
    for(int i=1;i<N+1;i++){
        distd[i]=-1;
    }
    distd[d]=0;
    while(!proc.empty()){
        int node=proc.front();
        proc.pop();
        for(int i=0;i<adjList[node].size();i++){
            if(distd[adjList[node][i]]==-1){
                distd[adjList[node][i]]=distd[node]+1;
                proc.push(adjList[node][i]);
            }
        }
    }
    proc.push(a);
    int dista[N+1];
    for(int i=1;i<N+1;i++){
        dista[i]=-1;
    }
    dista[a]=0;
    while(!proc.empty()){
        int node=proc.front();
        proc.pop();
        for(int i=0;i<adjList[node].size();i++){
            if(dista[adjList[node][i]]==-1){
                dista[adjList[node][i]]=dista[node]+1;
                proc.push(adjList[node][i]);
            }
        }
    }
    proc.push(b);
    int distb[N+1];
    for(int i=1;i<N+1;i++){
        distb[i]=-1;
    }
    distb[b]=0;
    while(!proc.empty()){
        int node=proc.front();
        proc.pop();
        for(int i=0;i<adjList[node].size();i++){
            if(distb[adjList[node][i]]==-1){
                distb[adjList[node][i]]=distb[node]+1;
                proc.push(adjList[node][i]);
            }
        }
    }
    int temps=N, tempd=N;
    if(dists[a]==dists[b]&&distd[a]==distd[b]){
        for(int i=1;i<N+1;i++){
            if(dista[i]==distb[i]){
                if(dists[i]+dista[i]==dists[a]){
                    temps=min(temps, dista[i]);
                }
                if(distd[i]+dista[i]==distd[a]){
                    tempd=min(tempd, dista[i]);
                }
            }
        }
    }
    printf("%d", max(min(distd[a]-dists[a], distd[b]-dists[b]), -1)+(temps>tempd?-1:0));
    return 0;
}

Compilation message

007.cpp: In function 'int main()':
007.cpp:26:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<adjList[node].size();i++){
                     ~^~~~~~~~~~~~~~~~~~~~~
007.cpp:42:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<adjList[node].size();i++){
                     ~^~~~~~~~~~~~~~~~~~~~~
007.cpp:58:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<adjList[node].size();i++){
                     ~^~~~~~~~~~~~~~~~~~~~~
007.cpp:74:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<adjList[node].size();i++){
                     ~^~~~~~~~~~~~~~~~~~~~~
007.cpp:8:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d%d%d", &N, &M, &s, &d, &a, &b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 3448 KB Output is correct
2 Correct 44 ms 4720 KB Output is correct
3 Correct 34 ms 3652 KB Output is correct
4 Correct 50 ms 4984 KB Output is correct
5 Correct 31 ms 3192 KB Output is correct
6 Correct 36 ms 3704 KB Output is correct
7 Correct 104 ms 3960 KB Output is correct
8 Correct 58 ms 3960 KB Output is correct
9 Correct 62 ms 4376 KB Output is correct
10 Correct 217 ms 9444 KB Output is correct
11 Correct 111 ms 6876 KB Output is correct
12 Correct 118 ms 8616 KB Output is correct
13 Correct 150 ms 7416 KB Output is correct
14 Correct 125 ms 6368 KB Output is correct
15 Correct 147 ms 8952 KB Output is correct
16 Correct 133 ms 9348 KB Output is correct
17 Correct 121 ms 8448 KB Output is correct
18 Correct 87 ms 8412 KB Output is correct
19 Correct 160 ms 9848 KB Output is correct
20 Correct 232 ms 12920 KB Output is correct
21 Correct 136 ms 12072 KB Output is correct
22 Correct 182 ms 10492 KB Output is correct
23 Correct 154 ms 11952 KB Output is correct
24 Correct 207 ms 11908 KB Output is correct
25 Correct 138 ms 11244 KB Output is correct
26 Correct 117 ms 10616 KB Output is correct
27 Correct 173 ms 12068 KB Output is correct
28 Correct 190 ms 11988 KB Output is correct
29 Correct 214 ms 12280 KB Output is correct
30 Correct 305 ms 13816 KB Output is correct
31 Correct 177 ms 13916 KB Output is correct
32 Correct 189 ms 11880 KB Output is correct
33 Correct 178 ms 12284 KB Output is correct
34 Correct 237 ms 12792 KB Output is correct
35 Correct 138 ms 12408 KB Output is correct
36 Correct 157 ms 12848 KB Output is correct
37 Correct 181 ms 14592 KB Output is correct
38 Correct 209 ms 14408 KB Output is correct
39 Correct 220 ms 14428 KB Output is correct
40 Correct 306 ms 15656 KB Output is correct
41 Correct 499 ms 17768 KB Output is correct