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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

using type = long long;
using P = pair<type, type>;
struct Edge {
  int to, cost;
  Edge(int a, long long b) {
    to = a;
    cost = b;
  }
};

class Dijkstra {
private:
  const type _INF = 100000000;
  int V; // 頂点数
  vector<type> d;
  vector<int> prev;
  vector<vector<Edge>> G;

public:
  Dijkstra(int v) {
    V = v + 1;
    d.resize(V);
    G.resize(V);
    prev.resize(V);
  }

  void shortest_path(int s) {
    priority_queue<P, vector<P>, greater<P>> que;
    d.assign(V, _INF);
    prev.assign(V, -1);
    d[s] = 0;
    que.push(P(0, s));
    while (que.size()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (auto e : G[v]) {
        if (d[v] + e.cost < d[e.to]) {
          d[e.to] = d[v] + e.cost;
          prev[e.to] = v;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }

  void add_path(int from, int to, type cost) {
    G[from].push_back(Edge(to, cost));
  }

  type get_cost(int t) { return d[t]; }

  bool enable_move(int t) { return d[t] != _INF; }

  vector<int> get_path(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t])
      path.push_back(t);
    reverse(path.begin(), path.end());
    return path;
  }
};

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;

  Dijkstra ds(n), dt(n);
  vector<int> x(m), y(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    ds.add_path(x[i], y[i], 1);
    ds.add_path(y[i], x[i], 1);
    dt.add_path(x[i], y[i], 1);
    dt.add_path(y[i], x[i], 1);
  }
  ds.shortest_path(s);
  dt.shortest_path(t);

  ll ds_cost[300005] = {0};
  for (int i = 1; i < n + 1; i++) {
    if (ds.enable_move(i))
      ds_cost[ds.get_cost(i)]++;
  }

  ll dt_cost[300005] = {0};
  for (int i = 1; i < n + 1; i++) {
    if (dt.enable_move(i))
      dt_cost[dt.get_cost(i)]++;
  }

  ll min_cost = ds.get_cost(t);
  ll ans = 0;
  for (int i = 0; i < min_cost - 1; i++) {
    ans += (ds_cost[i] * dt_cost[min_cost - 2 - i]);
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

using type = long long;
using P = pair<type, type>;
struct Edge {
  int to, cost;
  Edge(int a, long long b) {
    to = a;
    cost = b;
  }
};

class Dijkstra {
private:
  const type _INF = 100000000;
  int V; // 頂点数
  vector<type> d;
  vector<int> prev;
  vector<vector<Edge>> G;

public:
  Dijkstra(int v) {
    V = v + 1;
    d.resize(V);
    G.resize(V);
    prev.resize(V);
  }

  void shortest_path(int s) {
    priority_queue<P, vector<P>, greater<P>> que;
    d.assign(V, _INF);
    prev.assign(V, -1);
    d[s] = 0;
    que.push(P(0, s));
    while (que.size()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (auto e : G[v]) {
        if (d[v] + e.cost < d[e.to]) {
          d[e.to] = d[v] + e.cost;
          prev[e.to] = v;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }

  void add_path(int from, int to, type cost) {
    G[from].push_back(Edge(to, cost));
  }

  type get_cost(int t) { return d[t]; }

  bool enable_move(int t) { return d[t] != _INF; }

  vector<int> get_path(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t])
      path.push_back(t);
    reverse(path.begin(), path.end());
    return path;
  }
};

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;

  Dijkstra ds(n), dt(n);
  vector<int> x(n), y(n);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    ds.add_path(x[i], y[i], 1);
    ds.add_path(y[i], x[i], 1);
    dt.add_path(x[i], y[i], 1);
    dt.add_path(y[i], x[i], 1);
  }
  ds.shortest_path(s);
  dt.shortest_path(t);

  int ds_cost[300005] = {0};
  for (int i = 1; i < n + 1; i++) {
    if (ds.enable_move(i))
      ds_cost[ds.get_cost(i)]++;
  }

  int dt_cost[300005] = {0};
  for (int i = 1; i < n + 1; i++) {
    if (dt.enable_move(i))
      dt_cost[dt.get_cost(i)]++;
  }

  int min_cost = ds.get_cost(t);
  ll ans = 0;
  for (int i = 0; i < min_cost - 1; i++) {
    ans += (ds_cost[i] * dt_cost[min_cost - 2 - i]);
  }

  cout << ans << endl;
  return 0;
}