Problem p01720 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1LL << 60)
#define EPS 1e-10
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;

ll N, M;
vector<ll> G[100005];

vector<ll> dijkstra(ll s) {
  vector<ll> d(N, INF);

  priority_queue<pi, vector<pi>, greater<pi>> Q;
  Q.push(pi(0, s));
  d[s] = 0;
  while (!Q.empty()) {
    pi p = Q.top();
    Q.pop();
    ll n = p.second;
    if (d[n] < p.first)
      continue;
    for (auto g : G[n]) {
      if (d[g] > d[n] + 1) {
        Q.push(pi(d[n] + 1, g));
        d[g] = d[n] + 1;
      }
    }
  }
  return d;
}

int main() {
  ll s, t;
  scanf("%lld%lld%lld%lld", &N, &M, &s, &t);
  s--;
  t--;
  rep(i, M) {
    ll x, y;
    scanf("%lld%lld", &x, &y);
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  vector<ll> S = dijkstra(s);
  vector<ll> T = dijkstra(t);
  map<ll, ll> dist1;
  multiset<ll> dist2;
  for (auto x : S) {
    if (dist1.count(x))
      dist1[x] = dist1[x] + 1;
    else
      dist1[x] = 1;
  }
  for (auto x : T)
    dist2.insert(x);

  ll d = S[t];
  ll res = 0;
  for (auto x : dist1) {
    //    cout << x << " " << d - x - 2 << " " << dist.count(d-x-2) << endl;
    res += x.second * dist2.count(d - x.first - 2);
  }
  printf("%lld\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1LL << 60)
#define EPS 1e-10
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;

ll N, M;
vector<ll> G[100005];

vector<ll> dijkstra(ll s) {
  vector<ll> d(N, INF);
  priority_queue<pi, vector<pi>, greater<pi>> Q;
  Q.push(pi(0, s));
  d[s] = 0;
  while (!Q.empty()) {
    pi p = Q.top();
    Q.pop();
    ll n = p.second;
    if (d[n] < p.first)
      continue;
    for (auto g : G[n]) {
      if (d[g] >= d[n] + 1) {
        Q.push(pi(d[n] + 1, g));
        d[g] = d[n] + 1;
      }
    }
  }
  return d;
}

int main() {
  ll s, t;
  scanf("%lld%lld%lld%lld", &N, &M, &s, &t);
  s--;
  t--;
  rep(i, M) {
    ll x, y;
    scanf("%lld%lld", &x, &y);
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  vector<ll> S = dijkstra(s);
  vector<ll> T = dijkstra(t);

  multiset<ll> dist;
  for (auto x : T)
    dist.insert(x);

  ll d = S[t];
  ll res = 0;
  for (auto x : S) {
    //    cout << x << " " << d - x - 2 << " " << dist.count(d-x-2) << endl;
    res += dist.count(d - x - 2);
  }
  printf("%lld\\n", res);
  return 0;
}