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

#include <fstream>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

class state {
public:
  int now, cost;
  state(int now, int cost) : now(now), cost(cost) {}
};

bool operator<(state a, state b) { return a.cost > b.cost; }

int n, m, s, g1, g2;

int cost_s[101], cost_g1[101], cost_g2[101];

int edge[101][101];
int edge_rev[101][101];

int main() {
  std::ofstream ofs("ans.txt");
  while (cin >> n >> m >> s >> g1 >> g2 && n != 0) {
    int ans = 1000000;
    for (int i = 0; i < 101; i++) {
      cost_s[i] = cost_g1[i] = cost_g2[i] = 10000000;
      for (int j = 0; j < 101; j++) {
        edge[i][j] = 10000000;
        edge_rev[i][j] = 100000000;
      }
    }

    for (int i = 0; i < m; i++) {
      int in, out, c;
      cin >> in >> out >> c;
      edge[in][out] = c;
      edge_rev[out][in] = c;
    }

    priority_queue<state> Q;
    Q.push(state(s, 0));
    while (!Q.empty()) {
      state info = Q.top();
      Q.pop();
      if (info.cost >= cost_s[info.now])
        continue;
      cost_s[info.now] = info.cost;
      for (int i = 0; i < 101; i++) {
        Q.push(state(i, info.cost + edge[info.now][i]));
      }
    }

    Q.push(state(g1, 0));
    while (!Q.empty()) {
      state info = Q.top();
      Q.pop();
      if (info.cost >= cost_g1[info.now])
        continue;
      cost_g1[info.now] = info.cost;
      for (int i = 0; i < 101; i++) {
        Q.push(state(i, info.cost + edge_rev[info.now][i]));
      }
    }

    Q.push(state(g2, 0));
    while (!Q.empty()) {
      state info = Q.top();
      Q.pop();
      if (info.cost >= cost_g2[info.now])
        continue;
      cost_g2[info.now] = info.cost;
      for (int i = 0; i < 101; i++) {
        Q.push(state(i, info.cost + edge_rev[info.now][i]));
      }
    }

    for (int i = 0; i < 101; i++) {
      ans = min(ans, cost_s[i] + cost_g1[i] + cost_g2[i]);
    }
    cout << ans << endl;
    ofs << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

class state {
public:
  int now, cost;
  state(int now, int cost) : now(now), cost(cost) {}
};

bool operator<(state a, state b) { return a.cost < b.cost; }

int n, m, s, g1, g2;

class edge {
public:
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};

int main() {
  while (cin >> n >> m >> s >> g1 >> g2 && n != 0) {
    vector<vector<edge>> E(n + 1);
    for (int i = 0; i < m; i++) {
      int in, out, c;
      cin >> in >> out >> c;
      E[in].push_back(edge(out, c));
      E[out].push_back(edge(in, c));
    }
    int ans = 100000000;

    for (int i = 1; i <= n; i++) {
      int dist[101];
      for (int j = 0; j < 101; j++)
        dist[j] = 100000000;

      priority_queue<state> Q;
      Q.push(state(i, 0));
      while (!Q.empty()) {
        state t = Q.top();
        //				cout<<t.now<<" "<<t.cost<<endl;
        Q.pop();
        if (dist[t.now] < t.cost)
          continue;
        dist[t.now] = t.cost;
        for (int k = 0; k < E[t.now].size(); k++) {
          Q.push(state(E[t.now][k].to, t.cost + E[t.now][k].cost));
        }
      }
      //			cout<<dist[s]<<" "<<dist[g1]<<"
      //"<<dist[g2]<<endl;
      ans = min(ans, dist[s] + dist[g1] + dist[g2]);
    }
    cout << ans << endl;
  }
  return 0;
}