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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;

struct E {
  E(int dis, string to) : dis(dis), to(to) {}
  int dis;
  string to;
};
struct P {
  P(int calo, int visit, string pos) : calo(calo), visit(visit), pos(pos) {}
  int calo, visit;
  string pos;
};
bool operator>(const P &a, const P &b) { return a.calo > b.calo; }
int main() {
  int m, n, k, d;
  while (cin >> m >> n >> k >> d, m | n | k | d) {
    map<string, int> cake;
    for (int i = 1; i <= m; ++i)
      cin >> cake[string("C") + (char)('0' + i)];
    map<string, vector<E>> edge;
    for (int i = 0; i < d; ++i) {
      string a, b;
      int t;
      cin >> a >> b >> t;
      edge[a].push_back(E(t, b));
      edge[b].push_back(E(t, a));
    }
    map<string, map<int, int>> calo;
    queue<P> qq;
    priority_queue<P, vector<P>, greater<P>> q;
    calo["H"][0] = 0;
    q.push(P(0, 0, "H"));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      if (calo.count(p.pos) && calo[p.pos].count(p.calo) &&
          p.calo > calo[p.pos][p.visit])
        continue;

      foreach (it, edge[p.pos]) {
        int next_calo = p.calo + it->dis * k;
        int next_visit = p.visit;
        if (it->to[0] == 'C') {
          if (p.visit & 1 << (it->to[1] - '1'))
            continue;
          else {
            next_calo -= cake[it->to];
            next_visit |= 1 << (it->to[1] - '1');
          }
        }

        if (!calo.count(it->to) || !calo[it->to].count(next_visit) ||
            next_calo < calo[it->to][next_visit]) {
          calo[it->to][next_visit] = next_calo;
          q.push(P(next_calo, next_visit, it->to));
        }
      }
    }

    int res = 100000000;
    foreach (it, calo["D"])
      res = min(res, it->second);
    printf("%d\\n", res);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;

struct E {
  E(int dis, string to) : dis(dis), to(to) {}
  int dis;
  string to;
};
struct P {
  P(int calo, int visit, string pos) : calo(calo), visit(visit), pos(pos) {}
  int calo, visit;
  string pos;
};

bool operator>(const P &a, const P &b) { return a.calo < b.calo; }

int main() {
  int m, n, k, d;
  while (cin >> m >> n >> k >> d, m | n | k | d) {
    map<string, int> cake;
    for (int i = 1; i <= m; ++i)
      cin >> cake[string("C") + (char)('0' + i)];
    map<string, vector<E>> edge;
    for (int i = 0; i < d; ++i) {
      string a, b;
      int t;
      cin >> a >> b >> t;
      edge[a].push_back(E(t, b));
      edge[b].push_back(E(t, a));
    }
    map<string, map<int, int>> calo;
    queue<P> qq;
    priority_queue<P, vector<P>, greater<P>> q;
    calo["H"][0] = 0;
    q.push(P(0, 0, "H"));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      if (calo.count(p.pos) && calo[p.pos].count(p.calo) &&
          p.calo > calo[p.pos][p.visit])
        continue;

      foreach (it, edge[p.pos]) {
        int next_calo = p.calo + it->dis * k;
        int next_visit = p.visit;
        if (it->to[0] == 'C') {
          if (p.visit & 1 << (it->to[1] - '1'))
            continue;
          else {
            next_calo -= cake[it->to];
            next_visit |= 1 << (it->to[1] - '1');
          }
        }

        if (!calo.count(it->to) || !calo[it->to].count(next_visit) ||
            next_calo < calo[it->to][next_visit]) {
          calo[it->to][next_visit] = next_calo;
          q.push(P(next_calo, next_visit, it->to));
        }
      }
    }

    int res = 100000000;
    foreach (it, calo["D"])
      res = min(res, it->second);
    printf("%d\\n", res);
  }

  return 0;
}