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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int n, m, s, g1, g2;
  while (cin >> n >> m >> s >> g1 >> g2, n) {
    const int INF = 1 << 25;
    int dis[128][128];
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        dis[i][j] = i == j ? 0 : INF;
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      dis[a][b] = c;
    }
    for (int k = 1; k <= n; ++k)
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
          min_swap(dis[i][j], dis[i][k] + dis[k][j]);

    int res = INF;
    for (int k = 1; k <= n; ++k)
      min_swap(res, dis[s][k] + dis[k][g1] + dis[k][g2]);
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
int n, m, s, g1, g2;
vector<Edge> edge[128];
int dp[128][3];
bool used[128];
const int INF = 1 << 25;
void dfs(int v, int *res) {
  fill_n(res, 3, INF);

  if (dp[v][0] != -1) {
    copy(dp[v], dp[v] + 3, res);
    return;
  }

  if (v == g1)
    res[0] = 0;
  else if (v == g2)
    res[1] = 0;

  bool skip = false;
  used[v] = true;
  for (int i = 0; i < edge[v].size(); ++i) {
    if (used[edge[v][i].to]) {
      skip = true;
      continue;
    }

    int c[3];
    dfs(edge[v][i].to, c);
    for (int j = 0; j < 3; ++j)
      min_swap(res[j], c[j] + edge[v][i].cost);
  }
  used[v] = false;
  min_swap(res[2], res[0] + res[1]);

  if (!skip)
    copy(res, res + 3, dp[v]);
}
int main() {
  while (cin >> n >> m >> s >> g1 >> g2, n) {
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(Edge(b, c));
    }

    CLEAR(dp, -1);
    int res[3];
    dfs(s, res);
    cout << res[2] << endl;
  }
}