Problem p00224 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pr;
typedef pair<int, pr> ppr;
const int INF = 2 << 20;

int cake[7];
int m, n, k, d;
int number(string s) {
  if (s == "H")
    return 0;
  if (s == "D")
    return 1;
  if (s[0] == 'C')
    return s[1] - '0' + 1;
  else {
    int number = 0;
    for (int i = 1; i < s.size(); i++) {
      number *= 10;
      number += s[i] - '0';
    }
    return number + 10;
  }
  return 0;
}
int cakes(int j) {
  int sum = 0;
  for (int i = 0; i < 6; i++)
    if ((1 << i) & j)
      sum += cake[i];
  return sum;
}
int main() {
  // your code goes here
  while (cin >> m >> n >> k >> d && d != 0) {
    for (int i = 0; i < 6; i++)
      cake[i] = 0;
    string pointA, pointB;
    int dist = 0;
    int longs[120][2000] = {};
    for (int i = 0; i < 120; i++)
      for (int j = 0; j < 2000; j++)
        longs[i][j] = INF;
    longs[0][0] = 0;
    vector<pr> root[120];
    for (int i = 0; i < m; i++)
      cin >> cake[i];
    for (int i = 0; i < d; i++) {
      cin >> pointA >> pointB >> dist;
      root[number(pointA)].push_back(pr(dist, number(pointB)));
      root[number(pointB)].push_back(pr(dist, number(pointA)));
    }
    priority_queue<ppr, vector<ppr>, greater<ppr>> qu;
    qu.push(ppr(0, pr(0, 0)));
    while (!qu.empty()) {
      int dist = qu.top().first, s = qu.top().second.first,
          bits = qu.top().second.second;
      qu.pop();
      if (dist != longs[s][bits])
        continue;
      if (2 <= s && s <= 9)
        longs[s][bits | (int)pow(2, s - 2)] = dist;
      // cout << dist << " "  << s << " " << bits << " " <<endl;
      for (int i = 0; i < root[s].size(); i++) {
        int target = root[s][i].second;
        if (2 <= s && s <= 9 && (int)pow(2, target - 2) & bits)
          continue;
        int nextLong = dist + root[s][i].first;
        if (2 <= s && s <= 9) {
          int canum = bits | (int)pow(2, s - 2);
          if (nextLong < longs[target][canum]) {
            longs[target][canum] = nextLong;
            qu.push(ppr(nextLong, pr(target, canum)));
          }
        } else if (nextLong < longs[target][bits]) {
          longs[target][bits] = nextLong;
          qu.push(ppr(nextLong, pr(target, bits)));
        }
      }
    }
    int minim = INF;
    for (int i = 0; i < (1 << m + 1); i++)
      minim = min(minim, longs[1][i] * k - cakes(i));
    cout << minim << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pr;
typedef pair<int, pr> ppr;
const int INF = 2 << 28;

int cake[7];
int m, n, k, d;
int number(string s) {
  if (s == "H")
    return 0;
  if (s == "D")
    return 1;
  if (s[0] == 'C')
    return s[1] - '0' + 1;
  else {
    int number = 0;
    for (int i = 1; i < s.size(); i++) {
      number *= 10;
      number += s[i] - '0';
    }
    return number + 10;
  }
  return 0;
}
int cakes(int j) {
  int sum = 0;
  for (int i = 0; i < 6; i++)
    if ((1 << i) & j)
      sum += cake[i];
  return sum;
}
int main() {
  // your code goes here
  while (cin >> m >> n >> k >> d && d != 0) {
    string pointA, pointB;
    int dist;
    int longs[120][200] = {};
    for (int i = 0; i < 120; i++)
      for (int j = 0; j < 200; j++)
        if (i != 0)
          longs[i][j] = INF;
    vector<pr> root[120];
    for (int i = 0; i < m; i++)
      cin >> cake[i];
    for (int i = 0; i < d; i++) {
      cin >> pointA >> pointB >> dist;
      root[number(pointA)].push_back(pr(dist, number(pointB)));
      root[number(pointB)].push_back(pr(dist, number(pointA)));
    }
    priority_queue<ppr> qu;
    qu.push(ppr(0, pr(0, 0)));
    while (!qu.empty()) {
      int dist = qu.top().first, s = qu.top().second.first,
          bits = qu.top().second.second;
      qu.pop();
      if (dist != longs[s][bits])
        continue;
      for (int i = 0; i < root[s].size(); i++) {
        int nextLong = dist + root[s][i].first;
        if (nextLong < longs[root[s][i].second][bits]) {
          longs[root[s][i].second][bits] = nextLong;
          qu.push(ppr(nextLong, pr(root[s][i].second, bits)));
        }
        if (s >= 2 && s <= 9)
          if (nextLong < longs[root[s][i].second][bits + (int)pow(2, s - 2)]) {
            longs[root[s][i].second][bits + (int)pow(2, s - 2)] = nextLong;
            qu.push(ppr(nextLong, pr(root[s][i].second, bits + pow(2, s - 2))));
          }
      }
    }
    int minim = INF;
    for (int i = 0; i < (1 << m + 1); i++)
      minim = min(minim, longs[1][i] * k - cakes(i));
    cout << minim << endl;
  }
  return 0;
}