Problem p01135 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long LL;
typedef pair<LL, int> pli;

const LL INF = (LL)1e18;

LL len[33][33];
int next[33][33];
char label[64];

struct packet {
  string name;
  int from, to;
  LL tm;

  packet(int f, int to, LL tm, const char *s)
      : name(s), from(f), to(to), tm(tm) {}

  bool operator<(const packet &p) const {
    if (tm != p.tm) {
      return tm < p.tm;
    }
    return name < p.name;
  }
};

struct event {
  int type; //-1: human   other: packet
  int pos;
  LL tm;

  bool operator<(const event &e) const {
    if (tm != e.tm) {
      return tm > e.tm;
    }
    return type < e.type;
  }
};

void solve(int n, int m) {
  int f, t, d;

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      len[i][j] = INF;
      next[i][j] = 0;
    }
    len[i][i] = 0;
    next[i][i] = i;
  }

  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &f, &t, &d);
    len[f][t] = len[t][f] = d;
    next[f][t] = t;
    next[t][f] = f;
  }

  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) {
        if (i == k || k == j) {
          continue;
        }
        LL &d1 = len[i][j];
        LL d2 = len[i][k] + len[k][j];
        if ((d1 > d2) || (d1 == d2 && next[i][j] > next[i][k])) {
          d1 = d2;
          next[i][j] = next[i][k];
        }
      }

  vector<vector<int>> wait(n + 1);
  scanf("%d", &m);
  vector<packet> ps;
  priority_queue<event> pqev;
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d%s", &f, &t, &d, label);
    ps.push_back(packet(f, t, d, label));
    pqev.push((event){i, f, d});
  }

  vector<char> standby(n + 1, 1);

  while (!pqev.empty()) {
    event ev = pqev.top();
    pqev.pop();
    if (ev.type >= 0) {
      wait[ev.pos].push_back(ev.type);
      if (standby[ev.pos]) {
        standby[ev.pos] = 0;
        pqev.push((event){-1, ev.pos, ev.tm});
      }
    } else {
      vector<int> &ws = wait[ev.pos];
      if (ws.empty()) {
        standby[ev.pos] = 1;
        continue;
      }

      LL mintm = INF;
      for (int i = 0; i < ws.size(); ++i) {
        mintm = min(mintm, ps[ws[i]].tm);
      }

      int to = 999;
      for (int i = 0; i < ws.size(); ++i) {
        packet &p = ps[ws[i]];
        if (p.tm == mintm && to > next[p.from][p.to]) {
          to = next[p.from][p.to];
        }
      }
      vector<int> send;
      for (int i = 0; i < ws.size(); ++i) {
        packet &p = ps[ws[i]];
        if (p.tm <= ev.tm && next[ev.pos][p.to] == to) {
          send.push_back(i);
        }
      }

      LL arvtm = ev.tm + len[ev.pos][to];
      for (int i = send.size() - 1; i >= 0; --i) {
        int pn = ws[send[i]];
        packet &p = ps[pn];
        p.from = to;
        p.tm = arvtm;
        if (to != p.to) {
          pqev.push((event){pn, to, arvtm});
        }
        ws.erase(ws.begin() + send[i]);
      }

      pqev.push((event){-1, ev.pos, ev.tm + 2 * len[ev.pos][to]});
    }
  }

  sort(ps.begin(), ps.end());
  for (int i = 0; i < ps.size(); ++i) {
    printf("%s %lld\\n", ps[i].name.c_str(), ps[i].tm);
  }
}

int main() {
  bool flg = false;
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    if (flg) {
      puts("");
    }
    flg = true;
    solve(n, m);
  }
}

Error: TLE

Fixed submission:

// TLE
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;

struct packet {
  int tm, pos, dist;
  string name;
  packet(int t, int p, int d, const string &n)
      : tm(t), pos(p), dist(d), name(n) {}
};
vector<packet> pac;
int len[33][33], to[33][33], bac[33];
int tbl[1001];
const int inf = 1e9;
struct cmp {
  bool operator()(int x, int y) const {
    const packet &a = pac[x], &b = pac[y];
    return a.tm == b.tm ? to[a.pos][a.dist] > to[b.pos][b.dist] : a.tm > b.tm;
  }
};
bool cmp2(int x, int y) {
  const packet &a = pac[x], &b = pac[y];
  return a.tm == b.tm ? a.name < b.name : a.tm < b.tm;
}

int main() {
  int n, m, f, t, d, i, j, k, l;
  string name;
  priority_queue<int, vector<int>, cmp> qu;
  for (; cin >> n >> m, n;) {
    pac.clear();
    if (**len == 1)
      cout << endl;

    for (i = 0; i <= n; i++) {
      for (j = 0; j <= m; j++) {
        len[i][j] = inf;
        to[i][j] = inf;
      }
      bac[i] = len[i][i] = 0;
      //          to[i][i]=i;
    }

    for (; m--;) {
      cin >> f >> d >> t;
      len[f][d] = t;
      len[d][f] = t;
      to[f][d] = d;
      to[d][f] = f;
    }

    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++) {
          t = len[i][k] + len[k][j];
          if (len[i][j] > t) {
            len[i][j] = t;
            to[i][j] = to[i][k];
          }
        }
    cin >> l;
    for (j = 0; j < l; j++) {
      cin >> f >> d >> t >> name;
      pac.push_back(packet(t, f, d, name));
      qu.push(j);
    }

    for (; !qu.empty();) {
      int pi = qu.top();
      packet &p = pac[pi];
      t = p.tm;
      f = p.pos;
      if (t < bac[f]) {
        p.tm = bac[f];
        qu.pop();
        qu.push(pi);
      } else {
        d = to[f][p.dist];
        bac[f] = p.tm + 2 * len[p.pos][d];
        for (;;) {
          int qi = qu.top();
          packet &q = pac[qi];
          if (t == q.tm && f == q.pos && to[q.pos][q.dist] == d) {
            qu.pop();
            q.tm += len[f][d];
            q.pos = d;
            if (d != q.dist)
              qu.push(qi);
          } else
            break;
        }
      }
    }
    //      sort(pac.begin(),pac.end(),cmp2);
    //      for(j=0;j<l;j++)cout<<pac[j].name<<' '<<pac[j].tm<<endl;
    for (j = 0; j < l; j++)
      tbl[j] = j;
    sort(tbl, tbl + l, cmp2);
    for (j = 0; j < l; j++)
      printf("%s %d\\n", pac[tbl[j]].name.c_str(), pac[tbl[j]].tm);

    **len = 1;
  }
}