Problem p02361 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int N, M;

struct Edge {
  int src, dst, weight;
  Edge *next, *prev;
  Edge(int s, int d, int w, Edge *n, Edge *p)
      : src(s), dst(d), weight(w), next(n), prev(p) {}
  Edge() : Edge(0, 0, 0, nullptr, nullptr) {}

  void *operator new(size_t) {
    static char buf[1000010];
    static int i = 0;
    Edge *res = (Edge *)(buf + i);
    i += sizeof(Edge);
    return res;
  }
};

vector<Edge *> first;

void add_edge(int u, int v, int d) {
  Edge *next = first[u];
  first[u] = new Edge(u, v, d, next, nullptr);
  if (next != nullptr)
    next->prev = first[u];
}

int main() {
  int V, E, r;
  while (cin >> V >> E >> r) {
    first.assign(V, nullptr);
    for (int i = 0; i < E; ++i) {
      int s, t, d;
      cin >> s >> t >> d;
      add_edge(s, t, d);
    }

    using State = pair<int, int>;
    priority_queue<State> q;
    vector<long long> dist(V, 1e18);
    q.emplace(0, r);
    dist[r] = 0;

    while (q.size()) {
      long long d, v;
      tie(d, v) = q.top();
      q.pop();
      d = -d;
      if (dist[v] < d)
        continue;
      for (Edge *e = first[v]; e != nullptr; e = e->next) {
        if (dist[e->dst] > e->weight + dist[v]) {
          dist[e->dst] = e->weight + dist[v];
          q.emplace(-dist[e->dst], e->dst);
        }
      }
    }

    for (int i = 0; i < V; ++i) {
      if (dist[i] == 1e18)
        cout << "INF";
      else
        cout << dist[i];
      cout << '\n';
    }
  }

  // {
  //     vector<string> u, v;
  //     static char s[3000], t[3000];
  //     map<string,int> id;
  //     N = 0;
  //     M = 0;
  //     while(cin >> s >> t){
  //         u.push_back(s);
  //         v.push_back(t);
  //         if(id.count(s) == 0) id[s] = N++;
  //         if(id.count(t) == 0) id[t] = N++;
  //         ++M;
  //     }

  //     first.assign(N, nullptr);
  //     for(int i = 0; i < M; ++i){
  //         add_edge(id[u[i]], id[v[i]]);
  //     }
  // }
}

Fixed submission:
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int N, M;

struct Edge {
  int src, dst, weight;
  Edge *next, *prev;
  Edge(int s, int d, int w, Edge *n, Edge *p)
      : src(s), dst(d), weight(w), next(n), prev(p) {}
  Edge() : Edge(0, 0, 0, nullptr, nullptr) {}

  void *operator new(size_t) {
    static char buf[500010 * sizeof(Edge)];
    static int i = 0;
    Edge *res = (Edge *)(buf + i);
    i += sizeof(Edge);
    return res;
  }
};

vector<Edge *> first;

void add_edge(int u, int v, int d) {
  Edge *next = first[u];
  first[u] = new Edge(u, v, d, next, nullptr);
  if (next != nullptr)
    next->prev = first[u];
}

int main() {
  int V, E, r;
  while (cin >> V >> E >> r) {
    first.assign(V, nullptr);
    for (int i = 0; i < E; ++i) {
      int s, t, d;
      cin >> s >> t >> d;
      add_edge(s, t, d);
    }

    using State = pair<int, int>;
    priority_queue<State> q;
    vector<long long> dist(V, 1e18);
    q.emplace(0, r);
    dist[r] = 0;

    while (q.size()) {
      long long d, v;
      tie(d, v) = q.top();
      q.pop();
      d = -d;
      if (dist[v] < d)
        continue;
      for (Edge *e = first[v]; e != nullptr; e = e->next) {
        if (dist[e->dst] > e->weight + dist[v]) {
          dist[e->dst] = e->weight + dist[v];
          q.emplace(-dist[e->dst], e->dst);
        }
      }
    }

    for (int i = 0; i < V; ++i) {
      if (dist[i] == 1e18)
        cout << "INF";
      else
        cout << dist[i];
      cout << '\n';
    }
  }

  // {
  //     vector<string> u, v;
  //     static char s[3000], t[3000];
  //     map<string,int> id;
  //     N = 0;
  //     M = 0;
  //     while(cin >> s >> t){
  //         u.push_back(s);
  //         v.push_back(t);
  //         if(id.count(s) == 0) id[s] = N++;
  //         if(id.count(t) == 0) id[t] = N++;
  //         ++M;
  //     }

  //     first.assign(N, nullptr);
  //     for(int i = 0; i < M; ++i){
  //         add_edge(id[u[i]], id[v[i]]);
  //     }
  // }
}