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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i < (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n)-1; i >= (m); --i)
#define repc(i, n) for (int i = 0; i <= (n); ++i)
#define repcn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repcd(i, n) for (int i = (n); i >= 0; --i)
#define repcnd(i, m, n) for (int i = (n); i >= (m); --i)
#define repc0(i, n) for (i = 0; i <= (n); ++i)
#define repcn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repcd0(i, n) for (i = (n); i >= 0; --i)
#define repcnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define p_queue(n) priority_queue<n, vector<n>, greater<n>>
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1, 0};

int dis[123][123];
int n, m, s, g1, g2;

int main() {
  IL {
    cin >> n >> m >> s >> g1 >> g2;
    if (n + m + s + g1 + g2 == 0)
      break;
    --s, --g1, --g2;
    rep(i, n) rep(j, n) dis[i][j] = INF;
    rep(i, n) dis[i][i] = 0;
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      dis[a][b] = c;
    }
    rep(k, n) rep(i, n) rep(j, n) chmin(dis[i][j], dis[i][k] + dis[k][j]);
    int res = INF;
    rep(i, n) chmin(res, dis[s][i] + dis[i][g1] + dis[i][g2]);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i < (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n)-1; i >= (m); --i)
#define repc(i, n) for (int i = 0; i <= (n); ++i)
#define repcn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repcd(i, n) for (int i = (n); i >= 0; --i)
#define repcnd(i, m, n) for (int i = (n); i >= (m); --i)
#define repc0(i, n) for (i = 0; i <= (n); ++i)
#define repcn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repcd0(i, n) for (i = (n); i >= 0; --i)
#define repcnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define p_queue(n) priority_queue<n, vector<n>, greater<n>>
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1, 0};

int dis[123][123];
vpint road[123];
int n, m, s, g1, g2;

int main() {
  IL {
    cin >> n >> m >> s >> g1 >> g2;
    if (n + m + s + g1 + g2 == 0)
      break;
    --s, --g1, --g2;
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      road[a].PB(MP(b, c));
      road[b].PB(MP(a, c));
    }
    rep(i, n) rep(j, n) dis[i][j] = INF;
    rep(i, n) {
      priority_queue<pint, vector<pint>, greater<pint>> que;
      que.push(MP(0, i));
      while (!que.empty()) {
        pint now = que.top();
        que.pop();
        if (dis[i][now.second] <= now.first)
          continue;
        dis[i][now.second] = now.first;
        rep(j, sz(road[now.second])) {
          if (dis[i][road[now.second][j].first] <=
              now.first + road[now.second][j].second)
            continue;
          que.push(MP(now.first + road[now.second][j].second,
                      road[now.second][j].first));
        }
      }
    }
    int res = INF;
    rep(i, n) { chmin(res, dis[s][i] + dis[i][g1] + dis[i][g2]); }
    cout << res << endl;
  }
  return 0;
}