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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int c[10];
const int INF = 1 << 25;
int dist[200][1 << 10];

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

struct State {
  int v, bit, d;
  bool operator<(const State &x) const { return d > x.d; }
};

int main() {
  int M, N, K, D;
  while (cin >> M >> N >> K >> D, M + N + K + D) {
    for (int i = 0; i < M; i++) {
      cin >> c[i];
    }

    int s = M + N, g = s + 1;
    vector<vector<edge>> G(M + N + 2);
    for (int i = 0; i < D; i++) {
      char t;
      int a, b, x;
      cin >> t;
      if (t == 'H')
        a = s;
      else if (t == 'D')
        a = g;
      else if (t == 'L')
        cin >> a, a += M - 1;
      else
        cin >> a, a--;

      cin >> t;
      if (t == 'H')
        b = s;
      else if (t == 'D')
        b = g;
      else if (t == 'L')
        cin >> b, b += M - 1;
      else
        cin >> b, b--;

      cin >> x;

      // cerr << "a : " << a << ", b : " << b << ", x : " << x << endl;
      G[a].push_back({a, b, K * x});
      G[b].push_back({b, a, K * x});
    }

    fill((int *)begin(dist), (int *)end(dist), INF);
    dist[s][0] = 0;

    priority_queue<State> q;
    q.push({s, 0, 0});

    while (q.size()) {
      int v = q.top().v, bit = q.top().bit;
      int d = q.top().d;
      q.pop();
      if (dist[v][bit] < d)
        continue;
      for (auto e : G[v]) {
        int to = e.to;
        int cost = e.cost;
        int nb = bit;
        if (to < M) {
          if (~bit >> to & 1) {
            nb |= 1 << to;
            cost -= c[to];
          } else
            continue;
        }
        if (dist[to][nb] > d + cost) {
          dist[to][nb] = d + cost;
          q.push({to, nb, d + cost});
        }
      }
    }

    int ans = INF;
    for (int i = 0; i < 1 << M; i++) {
      ans = min(ans, dist[g][i]);
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

int c[6];
const int INF = 1 << 25;
int dist[200][1 << 6];

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int M, N, K, D;
  while (cin >> M >> N >> K >> D, M) {
    for (int i = 0; i < M; i++) {
      cin >> c[i];
    }

    int s = M + N, g = s + 1;
    vector<edge> G;
    for (int i = 0; i < D; i++) {
      char c;
      int a, b, x;
      cin >> c;
      if (c == 'H')
        a = s;
      else if (c == 'D')
        a = g;
      else if (c == 'L')
        cin >> a, a += M - 1;
      else
        cin >> a, a--;
      cin >> c;
      if (c == 'H')
        b = s;
      else if (c == 'D')
        b = g;
      else if (c == 'L')
        cin >> b, b += M - 1;
      else
        cin >> b, b--;
      cin >> x;

      // cerr << "a : " << a << ", b : " << b << ", x : " << x << endl;
      G.push_back({a, b, K * x});
      G.push_back({b, a, K * x});
    }

    fill((int *)begin(dist), (int *)end(dist), INF);
    dist[s][(1 << M) - 1] = 0;
    while (1) {
      bool update = false;
      for (auto e : G) {
        int from = e.from, to = e.to, cost = e.cost;
        for (int b = 0; b < 1 << M; b++) {
          if (dist[from][b] == INF)
            continue;
          int nb = b;
          if (to < M && (b >> to & 1)) {
            nb ^= 1 << to;
            cost -= c[to];
          }
          if (dist[to][nb] > dist[from][b] + cost) {
            dist[to][nb] = dist[from][b] + cost;
            update = true;
          }
        }
      }
      if (!update)
        break;
    }

    int ans = INF;
    for (int i = 0; i < 1 << M; i++) {
      ans = min(ans, dist[g][i]);
    }
    cout << ans << endl;
  }
}