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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

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

class State {
public:
  int id, cost;
  bool used;

  State(int _id, int _cost, bool _used) {
    id = _id;
    cost = _cost;
    used = _used;
  }

  bool operator<(const State &st) const { return cost > st.cost; }
};

int n, m;
bool closed[102][2];
vector<P> t[102];

void solve() {
  priority_queue<State> open;

  memset(closed, 0, sizeof(closed));
  open.push(State(0, 0, 0));

  while (!open.empty()) {
    State st = open.top();
    open.pop();

    if (closed[st.id][st.used])
      continue;
    closed[st.id][st.used] = true;

    if (st.id == n - 1) {
      cout << st.cost << endl;
      return;
    }

    for (int i = 0; i < t[st.id].size(); i++) {
      int to = t[st.id][i].first;
      int cost = t[st.id][i].second;
      open.push(State(to, st.cost + cost, st.used));
    }

    if (st.used)
      continue;

    for (int i = 0; i < t[st.id].size(); i++) {
      int a = t[st.id][i].first;

      for (int j = 0; j < t[a].size(); j++) {
        int b = t[a][j].first;

        open.push(State(b, st.cost, true));
      }
    }
  }

  while (true) {
  }
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++) {
      t[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      t[a].push_back(P(b, c));
      t[b].push_back(P(a, c));
    }

    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

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

class State {
public:
  int id, cost;
  bool used;

  State(int _id, int _cost, bool _used) {
    id = _id;
    cost = _cost;
    used = _used;
  }

  bool operator<(const State &st) const { return cost > st.cost; }
};

int n, m;
bool closed[102][2];
vector<P> t[102];

void solve() {
  priority_queue<State> open;

  memset(closed, 0, sizeof(closed));
  open.push(State(0, 0, 0));

  while (!open.empty()) {
    State st = open.top();
    open.pop();

    if (closed[st.id][st.used])
      continue;
    closed[st.id][st.used] = true;

    if (st.id == n - 1) {
      cout << st.cost << endl;
      return;
    }

    for (int i = 0; i < t[st.id].size(); i++) {
      int to = t[st.id][i].first;
      int cost = t[st.id][i].second;
      open.push(State(to, st.cost + cost, st.used));
    }

    if (st.used)
      continue;

    for (int i = 0; i < t[st.id].size(); i++) {
      int a = t[st.id][i].first;

      for (int j = 0; j < t[a].size(); j++) {
        int b = t[a][j].first;

        open.push(State(b, st.cost, true));
      }
    }
  }

  while (true) {
  }
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++) {
      t[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      t[a].push_back(P(b, c));
    }

    solve();
  }
}