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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

using namespace std;
#define INF 0x3f3f3f3f

// iPair ==> Integer Pair
typedef pair<int, int> iPair;

int dd[10000] = {
    0,
};

// This class represents a directed graph using
// adjacency list representation
class Graph {
  int V; // No. of vertices

  // In a weighted graph, we need to store vertex
  // and weight pair for every edge
  list<pair<int, int>> *adj;

public:
  Graph(int V); // Constructor

  // function to add an edge to graph
  void addEdge(int u, int v, int w);

  // prints shortest path from s
  void shortestPath(int s);
};

// Allocates memory for adjacency list
Graph::Graph(int V) {
  this->V = V;
  adj = new list<iPair>[V];
}

void Graph::addEdge(int u, int v, int w) {
  adj[u].push_back(make_pair(v, w));
  adj[v].push_back(make_pair(u, w));
}

// Prints shortest paths from src to all other vertices
void Graph::shortestPath(int src) {
  priority_queue<iPair, vector<iPair>, greater<iPair>> pq;

  // Create a vector for distances and initialize all
  // distances as infinite (INF)
  vector<int> dist(V, INF);
  // Insert source itself in priority queue and initialize
  // its distance as 0.
  pq.push(make_pair(0, src));
  dist[src] = 0;

  /* Looping till priority queue becomes empty (or all
  distances are not finalized) */
  while (!pq.empty()) {
    // The first vertex in pair is the minimum distance
    // vertex, extract it from priority queue.
    // vertex label is stored in second of pair (it
    // has to be done this way to keep the vertices
    // sorted distance (distance must be first item
    // in pair)
    int u = pq.top().second;
    pq.pop();

    // 'i' is used to get all adjacent vertices of a vertex
    list<pair<int, int>>::iterator i;
    for (i = adj[u].begin(); i != adj[u].end(); ++i) {
      // Get vertex label and weight of current adjacent
      // of u.
      int v = (*i).first;
      int weight = (*i).second;

      // If there is shorted path to v through u.
      if (dist[v] > dist[u] + weight) {
        // Updating distance of v
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }

  for (int i = 0; i < V; i++) {
    dd[dist[i]]++;
    // printf("%d->%d= %d\\n", src, i, dist[i]);
  }
}

// Driver program to test methods of graph class
int main() {
  // create the graph given in above fugure
  int n, x, y;
  cin >> n >> x >> y;
  Graph g(n);
  for (int i = 0; i < n - 1; i++) {
    g.addEdge(i, i + 1, 1);
  }
  g.addEdge(x - 1, y - 1, 1);
  for (int i = 0; i < n; i++) {
    dd[i] = 0;
  }
  for (int k = 0; k < n; k++) {
    g.shortestPath(k);
  }
  for (int i = 1; i < n; i++) {
    printf("%d\\n", dd[i] / 2);
  }
  return 0;
}


Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

using namespace std;
#define INF 0x3f3f3f3f

// iPair ==> Integer Pair
typedef pair<int, int> iPair;

vector<int> dd;

// This class represents a directed graph using
// adjacency list representation
class Graph {
  int V; // No. of vertices

  // In a weighted graph, we need to store vertex
  // and weight pair for every edge
  list<pair<int, int>> *adj;

public:
  Graph(int V); // Constructor

  // function to add an edge to graph
  void addEdge(int u, int v, int w);

  // prints shortest path from s
  void shortestPath(int s);
};

// Allocates memory for adjacency list
Graph::Graph(int V) {
  this->V = V;
  adj = new list<iPair>[V];
}

void Graph::addEdge(int u, int v, int w) {
  adj[u].push_back(make_pair(v, w));
  adj[v].push_back(make_pair(u, w));
}

// Prints shortest paths from src to all other vertices
void Graph::shortestPath(int src) {
  priority_queue<iPair, vector<iPair>, greater<iPair>> pq;

  // Create a vector for distances and initialize all
  // distances as infinite (INF)
  vector<int> dist(V, INF);
  dd = dist;
  // Insert source itself in priority queue and initialize
  // its distance as 0.
  pq.push(make_pair(0, src));
  dist[src] = 0;

  /* Looping till priority queue becomes empty (or all
  distances are not finalized) */
  while (!pq.empty()) {
    // The first vertex in pair is the minimum distance
    // vertex, extract it from priority queue.
    // vertex label is stored in second of pair (it
    // has to be done this way to keep the vertices
    // sorted distance (distance must be first item
    // in pair)
    int u = pq.top().second;
    pq.pop();

    // 'i' is used to get all adjacent vertices of a vertex
    list<pair<int, int>>::iterator i;
    for (i = adj[u].begin(); i != adj[u].end(); ++i) {
      // Get vertex label and weight of current adjacent
      // of u.
      int v = (*i).first;
      int weight = (*i).second;

      // If there is shorted path to v through u.
      if (dist[v] > dist[u] + weight) {
        // Updating distance of v
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }
}

// Driver program to test methods of graph class
int main() {
  // create the graph given in above fugure
  int n, x, y;
  cin >> n >> x >> y;
  vector<int> v;
  v.resize(20000);
  Graph g(n);
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      g.addEdge(i, i + 1, 1);
    }
    g.addEdge(x - 1, y - 1, 1);

    g.shortestPath(k);
    for (int j = 0; j < n; j++) {
      v[dd[j]]++;
    }
    dd.clear();
  }
  for (int i = 0; i < n; i++) {
    cout << v[i] << "\\n";
  }
  return 0;
}