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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int INF = INT_MAX / 3;
vector<vector<int>> d;

int main() {
  while (1) {
    int n, m, s, g1, g2;
    cin >> n >> m >> s >> g1 >> g2;
    if (n == 0)
      break;
    --s;
    --g1;
    --g2;
    d.assign(n, vector<int>(n, INF));

    int from, to, cost;
    for (int i = 0; i < m; ++i) {
      cin >> from >> to >> cost;
      --from;
      --to;
      d[from][to] = cost;
    }

    for (int i = 0; i < n; ++i)
      d[i][i] = 0;

    for (int k = 0; k < n; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    int ans = INF;
    for (int c = 0; c < n; ++c) {
      if (d[s][c] == INF || d[c][g1] == INF || d[c][g2] == INF)
        continue;
      int tmp = d[s][c] + d[c][g1] + d[c][g2];
      ans = min(tmp, ans);
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int INF = INT_MAX / 3;
vector<vector<int>> d;

int main() {
  while (1) {
    int n, m, s, g1, g2;
    cin >> n >> m >> s >> g1 >> g2;
    if (n == 0)
      break;
    d.assign(n, vector<int>(n, INF));

    int from, to, cost;
    for (int i = 0; i < m; ++i) {
      cin >> from >> to >> cost;
      d[from][to] = cost;
    }

    for (int k = 0; k < n; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    int ans = INF;
    for (int c = 0; c < n; ++c) {
      if (d[s][c] == INF || d[c][g1] == INF || d[c][g2] == INF)
        continue;
      int tmp = d[s][c] + d[c][g1] + d[c][g2];
      ans = min(tmp, ans);
    }

    cout << ans << endl;
  }
  return 0;
}