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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// #define int long long
#define LL long long
#define ULL unsigned long long

const LL LL_INF = ~(ULL)0 >> 1;

struct edge {
  int to, cost;
  edge() {}
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};

typedef pair<int, int> P;

signed main() {
  int n, k;
  while (cin >> n >> k, n) {
    vector<edge> gra[n + 1];
    for (int i = 0; i < k; ++i) {
      int flag;
      cin >> flag;

      if (flag) {
        int c, d, e;
        cin >> c >> d >> e;

        gra[c].push_back(edge(d, e));
        gra[d].push_back(edge(c, e));
      } else {
        int a, b;
        cin >> a >> b;
        LL d[n + 1];
        fill(d, d + n + 1, LL_INF);
        d[a] = 0;
        priority_queue<P, vector<P>, greater<P>> q;
        q.push(P(0, a));

        while (q.size()) {
          P p = q.top();
          q.pop();
          int v = p.second;
          if (d[v] < p.first)
            continue;

          for (int j = 0; j < gra[v].size(); ++j) {
            edge e = gra[v][j];
            if (d[v] + e.cost < d[e.to]) {
              d[e.to] = d[v] + e.cost;
              q.push(P(d[e.to], e.to));
            }
          }
        }

        printf("%ld\\n", ((d[b] == LL_INF) ? -1 : d[b]));
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// #define int long long
#define LL long long
#define ULL unsigned long long

const LL LL_INF = ~(ULL)0 >> 1;

struct state {
  int pos;
  long long money;
  bool n[101] = {false};

  state() {}
  state(int pos, long long money) {
    this->pos = pos;
    this->money = money;
    this->n[pos] = true;
  }
  state(int pos, long long money, bool *n) {
    this->pos = pos;
    this->money = money;
    for (int i = 0; i < 101; ++i) {
      this->n[i] = n[i];
    }
    this->n[pos] = true;
  }
};

signed main() {
  int n, k;
  while (scanf("%d%d", &n, &k), n) {
    int map[101][101] = {0};
    for (int i = 0; i < k; ++i) {
      int flag;
      scanf("%d", &flag);

      if (flag) {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        if ((map[c][d] == 0) || (e < map[c][d])) {
          map[c][d] = e;
          map[d][c] = e;
        }
      } else {
        int a, b;
        scanf("%d%d", &a, &b);

        stack<state> s;
        s.push(state(a, 0));

        long long min = LL_INF;
        while (s.size()) {
          state temp = s.top();
          s.pop();

          for (int j = 1; j <= n; ++j) {
            if ((map[temp.pos][j] == 0) || (temp.n[j]))
              continue;

            if (j == b) {
              int money = temp.money + map[temp.pos][j];
              if (money < min) {
                min = money;
              }
              continue;
            }
            s.push(state(j, temp.money + map[temp.pos][j], temp.n));
          }
        }
        printf("%d\\n", ((min == LL_INF) ? -1L : min));
      }
    }
  }
}