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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A) (A).begin(), (A).end()
#define DUMP(A) cout << #A << "=" << (A) << endl
#define SIZE(A) (int)((A).size())
using namespace std;
typedef long long ll;

int getnum(string s, int cake) {
  if (s[0] == 'C')
    return atoi(s.substr(1).c_str()) - 1;
  else if (s[0] == 'H')
    return cake;
  else if (s[0] == 'D')
    return cake + 1;
  else
    return cake + 1 + atoi(s.substr(1).c_str());
}
int main() {
  int m, n, k, d;
  for (;;) {
    scanf("%d%d%d%d", &m, &n, &k, &d);
    if (m == 0 && n == 0 && k == 0 && d == 0)
      break;
    int cals[6];
    for (int i = 0; i < m; i++) {
      cin >> cals[i];
    }
    pair<pair<int, int>, int> ps[512];
    for (int i = 0; i < d; i++) {
      string a, b;
      int d;
      cin >> a >> b >> d;
      ps[i * 2] = make_pair(make_pair(getnum(a, m), getnum(b, m)), d);
      ps[i * 2 + 1] = make_pair(make_pair(getnum(b, m), getnum(a, m)), d);
    }
    int cakepat = 1 << m, vnum = m + n + 2;
    int ds[108][64];
    const int INF = 10000000;
    for (int ix = 0; ix < vnum; ix++) {
      for (int i = 0; i < cakepat; i++) {
        ds[ix][i] = INF;
      }
    }
    ds[m][0] = 0;
    for (;;) {
      bool isc = false;
      for (int i = 0; i < d * 2; i++) {
        for (int j = 0; j < cakepat; j++) {
          int fromnum = ps[i].first.first, tonum = ps[i].first.second,
              d = ps[i].second;
          if (tonum < m) {
            if (j & (1 << tonum))
              continue;
            else {
              if (ds[tonum][j | (1 << tonum)] >
                  ds[fromnum][j] + d * k - cals[tonum]) {
                ds[tonum][j | (1 << tonum)] =
                    ds[fromnum][j] + d * k - cals[tonum];
                isc = true;
              }
            }
          } else {
            if (ds[tonum][j] > ds[fromnum][j] + d * k) {
              ds[tonum][j] = ds[fromnum][j] + d * k;
              isc = true;
            }
          }
        }
      }
      if (!isc)
        break;
    }
    int mind = INF;
    for (int i = 0; i < cakepat; i++) {
      mind = min(mind, ds[m + 1][i]);
    }
    printf("%d\\n", mind);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A) (A).begin(), (A).end()
#define DUMP(A) cout << #A << "=" << (A) << endl
#define SIZE(A) (int)((A).size())
using namespace std;
typedef long long ll;

int getnum(string s, int cake) {
  if (s[0] == 'C')
    return atoi(s.substr(1).c_str()) - 1;
  else if (s[0] == 'H')
    return cake;
  else if (s[0] == 'D')
    return cake + 1;
  else
    return cake + 1 + atoi(s.substr(1).c_str());
}
int main() {
  int m, n, k, d;
  for (;;) {
    scanf("%d%d%d%d", &m, &n, &k, &d);
    if (m == 0 && n == 0 && k == 0 && d == 0)
      break;
    int cals[6];
    for (int i = 0; i < m; i++) {
      cin >> cals[i];
    }
    pair<pair<string, string>, int> ps[512];
    for (int i = 0; i < d; i++) {
      string a, b;
      int d;
      cin >> a >> b >> d;
      ps[i * 2] = make_pair(make_pair(a, b), d);
      ps[i * 2 + 1] = make_pair(make_pair(b, a), d);
    }
    int cakepat = 1 << m, vnum = m + n + 2;
    int ds[108][64];
    const int INF = 10000000;
    for (int ix = 0; ix < vnum; ix++) {
      for (int i = 0; i < cakepat; i++) {
        ds[ix][i] = INF;
      }
    }
    ds[m][0] = 0;
    for (int ix = 0; ix < cakepat * vnum; ix++) {
      bool isc = false;
      for (int i = 0; i < d * 2; i++) {
        for (int j = 0; j < cakepat; j++) {
          string from = ps[i].first.first, to = ps[i].first.second;
          int fromnum = getnum(ps[i].first.first, m),
              tonum = getnum(ps[i].first.second, m);
          int d = ps[i].second;
          if (to[0] == 'C') {
            if (j & (1 << (to[1] - '1')))
              continue;
            else {
              ds[tonum][j | (1 << (to[1] - '1'))] =
                  min(ds[tonum][j | (1 << (to[1] - '1'))],
                      ds[fromnum][j] + d * k - cals[to[1] - '1']);
              isc = true;
            }
          } else {
            ds[tonum][j] = min(ds[tonum][j], ds[fromnum][j] + d * k);
            isc = true;
          }
        }
      }
      if (!isc)
        break;
    }
    int mind = INF;
    for (int i = 0; i < cakepat; i++) {
      mind = min(mind, ds[m + 1][i]);
    }
    printf("%d\\n", mind);
  }
}