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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int main() {
  while (true) {
    int n, m, s, g1, g2;
    int cost[101][101];
    cin >> n >> m >> s >> g1 >> g2;
    if (!n)
      break;
    FOR(i, 101) FOR(j, 101) cost[i][j] = INF;
    FOR(i, 101) cost[i][i] = 0;
    FOR(i, m) {
      int l, r, c;
      cin >> l >> r >> c;
      cost[l][r] = c;
    }

    FOR(k, n + 1) {
      FOR(i, n + 1) {
        FOR(j, n + 1) {
          if (cost[i][k] == INF || cost[k][j] == INF)
            continue;
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        }
      }
    }
    int ans = INF;
    FOR(k, n + 1) {
      ans = min({ans, cost[s][k] + cost[k][g1] + cost[k][g2],
                 cost[s][k] + cost[k][g1] + cost[g1][g2],
                 cost[s][k] + cost[k][g2] + cost[g2][g1]});
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

struct P {
  bool a[101] = {};
  int cost = 0;
  bool operator<(const P &right) const { return cost < right.cost; }
  bool operator>(const P &right) const { return cost > right.cost; }
};

int main() {
  while (true) {
    int n, m, s, g1, g2;
    int cost[101][101];
    cin >> n >> m >> s >> g1 >> g2;
    if (!n)
      break;
    FOR(i, 101) FOR(j, 101) cost[i][j] = INF;
    FOR(i, m) {
      int l, r, c;
      cin >> l >> r >> c;
      cost[l][r] = c;
    }
    P start;
    start.a[1] = true;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(start);

    while (!que.empty()) {
      P now = que.top();
      que.pop();
      if (now.a[g1] && now.a[g2]) {
        cout << now.cost << endl;
        break;
      }

      FORI(i, 1, n + 1) {
        if (now.a[i]) {
          FORI(j, 1, n + 1) {
            if (!now.a[j] && cost[i][j] != INF) {
              P next = now;
              next.a[j] = true;
              next.cost += cost[i][j];
              que.push(next);
            }
          }
        }
      }
    }
  }
}