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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  vector<int> to, c;
};

class Trl {
public:
  int p, c, pv, sec;
  Trl(int p, int c, int pv, int sec) : p(p), c(c), pv(pv), sec(sec) {}

  bool operator<(const Trl &t) const {
    if (c != t.c)
      return c > t.c;
    if (sec != t.sec)
      return sec > t.sec;
    return p > t.p;
  }
};

int nt[32][32], ld[32][32];
Node node[32];

void Dijkstra(int src, int dst) {
  priority_queue<Trl> q;
  q.push(Trl(src, 0, src, -1));

  int v[32];
  for (int i = 0; i < 32; i++)
    v[i] = -1;

  while (!q.empty()) {
    Trl t = q.top();
    q.pop();
    if (v[t.p] != -1)
      continue;
    v[t.p] = t.pv;

    if (t.p == dst) {
      nt[src][dst] = t.sec;
      break;
    }
    for (int i = 0; i < node[t.p].to.size(); i++) {
      int next = node[t.p].to[i];
      if (v[next] != -1)
        continue;

      q.push(
          Trl(next, t.c + node[t.p].c[i], t.p, (t.sec == -1 ? next : t.sec)));
    }
  }
}

enum EV { BACK, GO };

class Event {
public:
  EV ev;
  int p, dst, t, ot;
  string n;

  Event(int p, int dst, int t, int ot, EV ev, string n)
      : p(p), dst(dst), t(t), ot(ot), ev(ev), n(n) {}

  bool operator<(const Event &e) const {
    if (t != e.t)
      return t > e.t;
    if (ev != e.ev)
      return ev > e.ev;
    if (p == dst && e.p == e.dst)
      return n > e.n;
    if (ot != e.ot)
      return ot > e.ot;
    return nt[p][dst] > nt[e.p][e.dst];
  }
};

void Sim() {
  int K;
  scanf("%d", &K);
  priority_queue<Event> q;
  while (K--) {
    int s, d, t;
    char n[51];
    scanf("%d%d%d%s", &s, &d, &t, n);
    s--;
    d--;
    q.push(Event(s, d, t, t, GO, string(n)));
  }

  int lv[32] = {0};
  while (!q.empty()) {
    Event e = q.top();
    q.pop();
    if (e.ev == BACK) {
      lv[e.dst] = 0;
      continue;
    }

    if (e.p == e.dst) {
      printf("%s %d\\n", e.n.c_str(), e.t);
    } else {
      if (lv[e.p] != 0) {
        q.push(Event(e.p, e.dst, lv[e.p], e.ot, e.ev, e.n));
      } else {
        int next = nt[e.p][e.dst], cst = e.t + ld[e.p][next];
        lv[e.p] = cst + ld[e.p][next];
        q.push(Event(next, e.dst, cst, cst, e.ev, e.n));
        q.push(Event(e.p, e.p, lv[e.p], lv[e.p], BACK, ""));

        priority_queue<Event> tmp;
        while (!q.empty()) {
          Event te = q.top();
          if (te.t != e.t)
            break;
          if (next == nt[te.p][te.dst] && te.p == e.p) {
            q.push(Event(next, te.dst, cst, cst, te.ev, te.n));
          } else
            tmp.push(te);
          q.pop();
        }

        while (!tmp.empty()) {
          q.push(tmp.top());
          tmp.pop();
        }
      }
    }
  }
}

int main() {
  int N, M;
  bool fr = true;
  while (scanf("%d%d", &N, &M), (N || M)) {
    for (int i = 0; i < 32; i++) {
      node[i].to.clear();
      node[i].c.clear();
    }

    while (M--) {
      int s, d, c;
      scanf("%d%d%d", &s, &d, &c);
      s--;
      d--;

      node[s].to.push_back(d);
      node[d].to.push_back(s);
      node[s].c.push_back(c);
      node[d].c.push_back(c);
      ld[s][d] = c;
      ld[d][s] = c;
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        Dijkstra(i, j);

    if (!fr)
      puts("");
    fr = false;

    Sim();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  vector<int> to, c;
};

class Trl {
public:
  int p, c, pv;
  Trl(int p, int c, int pv) : p(p), c(c), pv(pv) {}

  bool operator<(const Trl &t) const {
    if (c != t.c)
      return c > t.c;
    return p > t.p;
  }
};

int nt[32][32], ld[32][32];
Node node[32];

void Dijkstra(int src, int dst) {
  priority_queue<Trl> q;
  q.push(Trl(src, 0, src));

  int v[32];
  for (int i = 0; i < 32; i++)
    v[i] = -1;

  while (1) {
    Trl t = q.top();
    q.pop();
    if (v[t.p] != -1)
      continue;
    v[t.p] = t.pv;

    if (t.p == dst) {
      for (int i = dst; v[i] != i; i = v[i])
        nt[v[i]][dst] = i;

      break;
    }

    for (int i = 0; i < node[t.p].to.size(); i++) {
      int next = node[t.p].to[i];
      if (v[next] != -1)
        continue;

      q.push(Trl(next, t.c + node[t.p].c[i], t.p));
    }
  }
}

enum EV { BACK, GO };

class Event {
public:
  EV ev;
  int p, dst, t;
  string n;

  Event(int p, int dst, int t, EV ev, string n)
      : p(p), dst(dst), t(t), ev(ev), n(n) {}

  bool operator<(const Event &e) const {
    if (t != e.t)
      return t > e.t;
    if (ev != e.ev)
      return ev > e.ev;
    return n > e.n;
  }
};

void Sim() {
  int K;
  scanf("%d", &K);
  priority_queue<Event> q;
  while (K--) {
    int s, d, t;
    char n[51];
    scanf("%d%d%d%s", &s, &d, &t, n);
    s--;
    d--;
    q.push(Event(s, d, t, GO, string(n)));
  }

  int lv[32] = {0};
  while (!q.empty()) {
    Event e = q.top();
    q.pop();
    if (e.ev == BACK) {
      lv[e.dst] = 0;
      continue;
    }

    if (e.dst == e.p) {
      printf("%s %d\\n", e.n.c_str(), e.t);
    } else {
      if (lv[e.p] != 0) {
        q.push(Event(e.p, e.dst, lv[e.p], e.ev, e.n));
      } else {
        int next = nt[e.p][e.dst], cst = e.t + ld[e.p][next];
        lv[e.p] = cst + ld[e.p][next];
        q.push(Event(next, e.dst, cst, e.ev, e.n));
        q.push(Event(e.p, e.p, lv[e.p], BACK, ""));
      }
    }
  }
}

int main() {
  int N, M;
  bool fr = true;
  while (scanf("%d%d", &N, &M), (N || M)) {
    for (int i = 0; i < 32; i++) {
      node[i].to.clear();
      node[i].c.clear();
    }
    while (M--) {
      int s, d, c;
      scanf("%d%d%d", &s, &d, &c);
      s--;
      d--;

      node[s].to.push_back(d);
      node[d].to.push_back(s);
      node[s].c.push_back(c);
      node[d].c.push_back(c);
      ld[s][d] = c;
      ld[d][s] = c;
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        Dijkstra(i, j);

    if (!fr)
      puts("");
    fr = false;

    Sim();
  }
}