Language: C++

#include <iostream>
#include <map>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

typedef struct {
    int from, to;
    int cost;
} edge;

int main() {
    while (true) {
        int b_goal[2];
        int n_bases, n_roads, b_start;
        cin >> n_bases >> n_roads >> b_start >> b_goal[0] >> b_goal[1];

        if (n_bases == 0) break;

        map< int, vector<edge> > edges;
        map< int, vector<edge> > redges;

        while (n_roads--) {
            int b_from, b_to, cost;
            cin >> b_from >> b_to >> cost;
            edge e = { b_from, b_to, cost };
            edges[b_from].push_back(e);
            redges[b_to].push_back(e);
        }

        map<int, int> cost_from_start;
        for (int b = 1; b <= n_bases; ++b) {
            cost_from_start[b] = INT_MAX;
        }

        queue<edge> q;

        {
            vector<edge> ee = edges[b_start];
            for (size_t i = 0; i < ee.size(); ++i) {
                q.push(ee[i]);
            }
        }

        cost_from_start[b_start] = 0;
        for (; !q.empty(); q.pop()) {
            edge e = q.front();

            int new_cost = cost_from_start[e.from] == INT_MAX ? INT_MAX : cost_from_start[e.from] + e.cost;
            if (new_cost < cost_from_start[e.to]) {
                cost_from_start[e.to] = new_cost;

                vector<edge> ee = edges[e.to];
                for (size_t i = 0; i < ee.size(); ++i) {
                    q.push(ee[i]);
                }
            }
        }

        map<int, vector<int> > costs_from_goal;
        for (int b = 1; b <= n_bases; ++b) {
            costs_from_goal[b] = vector<int>(2, INT_MAX);
        }

        for (int I = 0; I <= 1; I++) {
            vector<edge> ee = redges[b_goal[I]];
            for (size_t i = 0; i < ee.size(); ++i) {
                q.push(ee[i]);
            }

            costs_from_goal[b_goal[I]][I] = 0;
            for (; !q.empty(); q.pop()) {
                edge e = q.front();

                int new_cost = costs_from_goal[e.to][I] == INT_MAX ? INT_MAX : costs_from_goal[e.to][I] + e.cost;
                if (new_cost < costs_from_goal[e.from][I]) {
                    costs_from_goal[e.from][I] = new_cost;

                    vector<edge> ee = redges[e.from];
                    for (size_t i = 0; i < ee.size(); ++i) {
                        q.push(ee[i]);
                    }
                }
            }
        }

        int min = INT_MAX;
        for (int b = 1; b <= n_bases; ++b) {
            if (cost_from_start[b] == INT_MAX) continue;
            if (costs_from_goal[b][0] == INT_MAX) continue;
            if (costs_from_goal[b][1] == INT_MAX) continue;

            int v = cost_from_start[b] + costs_from_goal[b][0] + costs_from_goal[b][1];
            if (v < min) {
                min = v;
            }
        }
        cout << min << endl;
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:35:34: error: 'INT_MAX' was not declared in this scope
   35 |             cost_from_start[b] = INT_MAX;
      |                                  ^~~~~~~
a.cc:6:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    5 | #include <queue>
  +++ |+#include <climits>
    6 | 
a.cc:51:55: error: 'INT_MAX' was not declared in this scope
   51 |             int new_cost = cost_from_start[e.from] == INT_MAX ? INT_MAX : cost_from_start[e.from] + e.cost;
      |                                                       ^~~~~~~
a.cc:51:55: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:64:49: error: 'INT_MAX' was not declared in this scope
   64 |             costs_from_goal[b] = vector<int>(2, INT_MAX);
      |                                                 ^~~~~~~
a.cc:64:49: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:77:60: error: 'INT_MAX' was not declared in this scope
   77 |                 int new_cost = costs_from_goal[e.to][I] == INT_MAX ? INT_MAX : costs_from_goal[e.to][I] + e.cost;
      |                                                            ^~~~~~~
a.cc:77:60: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:89:19: error: 'INT_MAX' was not declared in this scope
   89 |         int min = INT_MAX;
      |                   ^~~~~~~
a.cc:89:19: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'