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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long

const int maxn = 1e5 + 5;

int n, m;
bool bo[maxn];
int a[maxn], b[maxn], c[maxn];

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

struct node {
  ll v;
  int u;

  node() {}

  node(int _u, ll _v) { u = _u, v = _v; }

  bool operator<(const node &a) const { return v < a.v; }
};

struct Heap {
  int tot;
  node tree[maxn];

  void ins(node x) {
    tree[++tot] = x;
    int pos = tot;
    while (pos > 1) {
      if (tree[pos] < tree[pos >> 1])
        swap(tree[pos], tree[pos >> 1]), pos >>= 1;
      else
        break;
    }
  }

  int pop() {
    node res = tree[1];
    tree[1] = tree[tot--];
    int pos = 1, son = 2;
    while (son <= tot) {
      if (son < tot && tree[son | 1] < tree[son])
        son |= 1;
      if (tree[son] < tree[pos])
        swap(tree[son], tree[pos]), pos = son;
      else
        break;
      son = pos << 1;
    }
    return res.u;
  }
} T;

struct graph {
  int tot, h, t;
  ll dis[maxn], cnt[maxn];
  int list[maxn], deg[maxn];
  int now[maxn], pre[maxn], son[maxn], val[maxn];

  void clear() {
    tot = 0;
    memset(now, 0, sizeof(now));
    memset(deg, 0, sizeof(deg));
  }

  void add(int a, int b, int c) {
    pre[++tot] = now[a];
    now[a] = tot;
    son[tot] = b;
    val[tot] = c;
    deg[b]++;
  }

  void dij(int st) {
    memset(dis, 63, sizeof(dis));
    dis[st] = 0;
    T.ins(node(st, 0));
    while (T.tot) {
      int u = T.pop();
      for (int p = now[u], v = son[p]; p; p = pre[p], v = son[p])
        if (dis[v] > dis[u] + val[p]) {
          dis[v] = dis[u] + val[p];
          T.ins(node(v, dis[v]));
        }
    }
  }

  void calc(int st) {
    cnt[st] = 1;
    list[0] = st;
    h = 0, t = 1;
    while (h != t) {
      int u = list[h++];
      for (int p = now[u], v = son[p]; p; p = pre[p], v = son[p]) {
        cnt[v] += cnt[u];
        if (--deg[v] == 0)
          list[t++] = v;
      }
    }
  }
} G1, G2;

int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    a[i] = read(), b[i] = read(), c[i] = read();
    G1.add(a[i], b[i], c[i]);
    G2.add(b[i], a[i], c[i]);
  }
  G1.dij(1), G2.dij(2);
  G1.clear(), G2.clear();
  for (int i = 1; i <= m; i++)
    if (G1.dis[a[i]] + G2.dis[b[i]] + c[i] == G1.dis[2]) {
      bo[i] = 1;
      G1.add(a[i], b[i], c[i]), G2.add(b[i], a[i], c[i]);
    }
  G1.calc(1), G2.calc(2);
  for (int i = 1; i <= m; i++) {
    if (bo[i]) {
      if (G1.cnt[a[i]] * G2.cnt[b[i]] == G1.cnt[2])
        puts("SAD");
      else
        puts("SOSO");
    } else {
      if (G1.dis[b[i]] + c[i] + G2.dis[a[i]] < G1.dis[2])
        puts("HAPPY");
      else
        puts("SOSO");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

const int maxn = 100005;
const int maxm = 100005;

using namespace std;

int n, m;
struct edge {
  int to, w;
};
struct data {
  int u, v, w;
} q[maxm];

vector<edge> son[maxn];

int dis[maxn], vis[maxn], U, V, W;
int spfa() {
  queue<int> Q;
  memset(vis, 0, sizeof(vis));
  memset(dis, 0x3f3f3f, sizeof(dis));
  dis[1] = 0;
  vis[1] = 1;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = 0;
    int sz = son[u].size();
    for (int i = 0; i < sz; i++) {
      int v = son[u][i].to, w = son[u][i].w;
      if (U == u && V == v)
        continue;
      if (dis[u] + w < dis[v]) {
        dis[v] = dis[u] + w;
        if (!vis[v]) {
          vis[v] = 1;
          Q.push(v);
        }
      }
    }
  }
  return dis[2];
}

int main() {
  scanf("%d%d", &n, &m);
  int u, v, w;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    q[i] = (data){u, v, w};
    edge x = (edge){v, w};
    son[u].push_back(x);
  }
  U = -1, V = -1;
  int mindis = spfa();
  for (int i = 0; i < m; i++) {
    U = q[i].u, V = q[i].v, W = q[i].w;
    son[V].push_back((edge){U, W});
    int dis = spfa();
    son[V].erase(son[V].end() - 1);
    if (dis < mindis) {
      printf("HAPPY\\n");
    }
    if (dis == mindis) {
      printf("SOSO\\n");
    }
    if (dis > mindis) {
      printf("SAD\\n");
    }
  }
  return 0;
}