Submission #82418

#TimeUsernameProblemLanguageResultExecution timeMemory
82418Flying_dragon_02007 (CEOI14_007)C++14
0 / 100
363 ms16568 KiB
#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, dist[2][N];

const int inf = 1e8;

vector<int> graph[N];

queue<ii> pq;

int main() {
    //freopen("spy.inp", "r", stdin);
    //freopen("spy.out", "w", stdout);
    scanf("%d%d", &n, &m);
    scanf("%d%d%d%d", &s, &d, &a, &b);
    for(int i = 1; i <= n; i++)
        dist[0][i] = dist[1][i] = inf;
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        graph[u].pb(v);
        graph[v].pb(u);
    }
    dist[0][s] = 0;
    pq.push({dist[0][s], s});
    while(!pq.empty()) {
        ii frt = pq.front();
        pq.pop();
        int u = frt.se;
        if(frt.fi > dist[0][u]) continue;
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(dist[0][u] + 1 < dist[0][v]) {
                dist[0][v] = dist[0][u] + 1;
                pq.push({dist[0][v], v});
            }
        }
    }
    dist[1][d] = 0;
    pq.push({dist[1][d], d});
    while(!pq.empty()) {
        ii frt = pq.front();
        pq.pop();
        int u = frt.se;
        if(frt.fi > dist[1][u]) continue;
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(dist[1][u] + 1 < dist[1][v]) {
                dist[1][v] = dist[1][u] + 1;
                pq.push({dist[1][v], v});
            }
        }
    }
    if(dist[1][a] >= dist[0][a] && dist[1][b] >= dist[0][b])
        printf("%d", min(dist[1][a] - dist[0][a], dist[1][b] - dist[0][b]));
    else
        printf("-1");
}

Compilation message (stderr)

007.cpp: In function 'int main()':
007.cpp:42:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
007.cpp:57:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
007.cpp:25: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:26: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:31: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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...