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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 3100

/* Dijkstra O(NlogM)*/

struct Dijkstra {

  typedef int Type;

  int V;
  vector<vector<pair<int, Type>>> G;
  vector<int> cost;

  Dijkstra(int n) : V(n), G(n, vector<pair<int, Type>>()) {}

  void add_edge(int u, int v, Type c) { G[u].push_back({v, c}); }

  Type solve(int s, int g = -1) {
    cost.assign(V, -1);
    priority_queue<pair<Type, int>> pq;
    Type max_cost = 0;

    pq.push({0, s});

    while (pq.size()) {
      Type now_cost = pq.top().first;
      int now = pq.top().second;
      pq.pop();

      if (cost[now] >= 0)
        continue;

      cost[now] = -now_cost;
      max_cost = max(max_cost, -now_cost);

      if (now == g)
        return -now_cost;

      for (int i = 0; i < (int)G[now].size(); i++) {
        pq.push({now_cost - G[now][i].second, G[now][i].first});
      }
    }

    return max_cost;
  }
};

int main() {
  int n, r;
  int s[SIZE], t[SIZE], d[SIZE];
  bool ans[SIZE] = {};

  scanf("%d%d", &n, &r);

  Dijkstra dij(n);

  for (int i = 0; i < r; i++) {
    scanf("%d%d%d", s + i, t + i, d + i);
    s[i]--;
    t[i]--;

    dij.add_edge(t[i], s[i], d[i]);
    dij.add_edge(s[i], t[i], d[i]);
  }

  int ans_dis = 0;

  for (int i = 0; i < n; i++) {
    int max_dis = dij.solve(i);

    if (max_dis > ans_dis) {
      for (int j = 0; j < n; j++)
        ans[j] = false;
      ans_dis = max_dis;
    }

    if (max_dis < ans_dis)
      continue;

    queue<int> que;
    bool visited[SIZE] = {};

    for (int j = 0; j < n; j++) {
      if (max_dis == dij.cost[j]) {
        que.push(j);
      }
    }

    while (que.size()) {
      int p = que.front();
      que.pop();

      if (visited[p])
        continue;
      visited[p] = true;
      ans[p] = true;

      for (int j = 0; j < dij.G[p].size(); j++) {
        if (dij.cost[p] - dij.G[p][j].second == dij.cost[dij.G[p][j].first]) {
          que.push(dij.G[p][j].first);
        }
      }
    }
  }

  int ans_count = 0;
  for (int i = 0; i < n; i++)
    ans_count += ans[i] == false;

  printf("%d\\n", ans_count);

  for (int i = 0; i < n; i++) {
    if (ans[i] == false) {
      printf("%d\\n", i + 1);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 110

/* Dijkstra O(NlogM)*/

struct Dijkstra {

  typedef int Type;

  int V;
  vector<vector<pair<int, Type>>> G;
  vector<int> cost;

  Dijkstra(int n) : V(n), G(n, vector<pair<int, Type>>()) {}

  void add_edge(int u, int v, Type c) { G[u].push_back({v, c}); }

  Type solve(int s, int g = -1) {
    cost.assign(V, -1);
    priority_queue<pair<Type, int>> pq;
    Type max_cost = 0;

    pq.push({0, s});

    while (pq.size()) {
      Type now_cost = pq.top().first;
      int now = pq.top().second;
      pq.pop();

      if (cost[now] >= 0)
        continue;

      cost[now] = -now_cost;
      max_cost = max(max_cost, -now_cost);

      if (now == g)
        return -now_cost;

      for (int i = 0; i < (int)G[now].size(); i++) {
        pq.push({now_cost - G[now][i].second, G[now][i].first});
      }
    }

    return max_cost;
  }
};

int main() {
  int n, r;
  int s[SIZE], t[SIZE], d[SIZE];
  bool ans[SIZE] = {};

  scanf("%d%d", &n, &r);

  Dijkstra dij(n);

  for (int i = 0; i < r; i++) {
    scanf("%d%d%d", s + i, t + i, d + i);
    s[i]--;
    t[i]--;

    dij.add_edge(t[i], s[i], d[i]);
    dij.add_edge(s[i], t[i], d[i]);
  }

  int ans_dis = 0;

  for (int i = 0; i < n; i++) {
    int max_dis = dij.solve(i);

    if (max_dis > ans_dis) {
      for (int j = 0; j < n; j++)
        ans[i] = false;
      ans_dis = max_dis;
    }

    if (max_dis < ans_dis)
      continue;

    queue<int> que;
    bool visited[SIZE] = {};

    for (int j = 0; j < n; j++) {
      if (max_dis == dij.cost[j]) {
        que.push(j);
      }
    }

    while (que.size()) {
      int p = que.front();
      que.pop();

      if (visited[p])
        continue;
      visited[p] = true;
      ans[p] = true;

      for (int j = 0; j < dij.G[p].size(); j++) {
        if (dij.cost[p] - dij.G[p][j].second == dij.cost[dij.G[p][j].first]) {
          que.push(dij.G[p][j].first);
        }
      }
    }
  }

  int ans_count = 0;
  for (int i = 0; i < n; i++)
    ans_count += ans[i] == false;

  printf("%d\\n", ans_count);

  for (int i = 0; i < n; i++) {
    if (ans[i] == false) {
      printf("%d\\n", i + 1);
    }
  }

  return 0;
}