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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

typedef pair<string, int> P;
int n, q, m;

vector<P> v[11];
vector<string> name;
map<string, int> sale;
int root[11][11];
const int INF = 10000000;
int dpc[11][1 << 15];
int dpd[11][1 << 15];
int ac;
int ad;

void rec(int x, int bit, int c, int d) {
  if (dpc[x][bit] < c)
    return;
  if (dpc[x][bit] == c && dpd[x][bit] < d)
    return;
  dpc[x][bit] = c;
  dpd[x][bit] = d;
  int nbit = bit;
  int nc = c;
  for (int i = 0; i < v[x].size(); i++) {
    for (int j = 0; j < name.size(); j++) {
      if (name[j] == v[x][i].first && !(bit >> j & 1) &&
          sale[name[j]] == v[x][i].second) {
        nbit |= 1 << j;
        nc += v[x][i].second;
      }
    }
  }
  if (nbit == (1 << q) - 1) {
    if (nc < ac || (nc == ac && d + root[0][x] < ad)) {
      ac = nc;
      ad = d + root[0][x];
    }
  } else {
    for (int i = 1; i <= n; i++) {
      int nd = d + root[i][x];
      if (dpc[i][nbit] > nc || (dpc[i][nbit] == nc && dpd[i][nbit] > nd)) {
        dpc[i][nbit] = nc;
        dpd[i][nbit] = nd;
        rec(i, nbit, nc, nd);
      }
    }
  }
}

int main(void) {
  while (1) {
    ac = INF;
    ad = INF;
    bool ch = true;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        root[i][j] = INF;
      }
      root[i][i] = 0;
      v[i].clear();
    }
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= (1 << 15) - 1; j++) {
        dpc[i][j] = INF;
        dpd[i][j] = INF;
      }
    }
    name.clear();
    sale.clear();
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        string str;
        int cost;
        cin >> str >> cost;
        v[i].push_back(P(str, cost));
        if (sale[str] == 0)
          sale[str] = cost;
        else
          sale[str] = min(cost, sale[str]);
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      string str;
      cin >> str;
      name.push_back(str);
      if (sale[str] == 0)
        ch = false;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      int s, t, d;
      cin >> s >> t >> d;
      root[s][t] = min(d, root[s][t]);
      root[t][s] = min(d, root[t][s]);
    }
    for (int k = 0; k <= n; k++) {
      for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
          root[i][j] = min(root[i][j], root[i][k] + root[k][j]);
        }
      }
    }
    if (ch)
      rec(0, 0, 0, 0);
    if (ac >= INF || ch == false)
      cout << "impossible" << endl;
    else
      cout << ac << " " << ad << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

typedef pair<string, int> P;
int n, q, m;

vector<P> v[11];
vector<string> name;
map<string, int> sale;
int root[11][11];
const int INF = 10000000;
int dpc[11][1 << 15];
int dpd[11][1 << 15];
int ac;
int ad;

void rec(int x, int bit, int c, int d) {
  if (dpc[x][bit] < c)
    return;
  if (dpc[x][bit] == c && dpd[x][bit] < d)
    return;
  dpc[x][bit] = c;
  dpd[x][bit] = d;
  if (bit == (1 << q) - 1 && x == 0) {
    if (c < ac || (c == ac && d < ad)) {
      ac = c;
      ad = d;
    }
  } else {
    for (int i = 0; i < v[x].size(); i++) {
      for (int j = 0; j < name.size(); j++) {
        if (name[j] == v[x][i].first && !(bit >> j & 1) &&
            sale[name[j]] == v[x][i].second) {
          int nbit = bit | 1 << j;
          int nc = c + v[x][i].second;
          if (dpc[x][nbit] > nc || (dpc[x][nbit] == nc && dpd[x][nbit] > d)) {
            dpc[x][nbit] = nc;
            dpd[x][nbit] = d;
            rec(x, nbit, nc, d);
          }
        }
      }
    }
    for (int i = 0; i <= n; i++) {
      int nd = d + root[i][x];
      if (dpc[i][bit] > c || (dpc[i][bit] == c && dpd[i][bit] > nd)) {
        dpc[i][bit] = c;
        dpd[i][bit] = nd;
        rec(i, bit, c, nd);
      }
    }
  }
}

int main(void) {
  while (1) {
    ac = INF;
    ad = INF;
    bool ch = true;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        root[i][j] = INF;
      }
      root[i][i] = 0;
      v[i].clear();
    }
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= (1 << 15) - 1; j++) {
        dpc[i][j] = INF;
        dpd[i][j] = INF;
      }
    }
    name.clear();
    sale.clear();
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        string str;
        int cost;
        cin >> str >> cost;
        v[i].push_back(P(str, cost));
        if (sale[str] == 0)
          sale[str] = cost;
        else
          sale[str] = min(cost, sale[str]);
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      string str;
      cin >> str;
      name.push_back(str);
      if (sale[str] == 0)
        ch = false;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      int s, t, d;
      cin >> s >> t >> d;
      root[s][t] = min(d, root[s][t]);
      root[t][s] = min(d, root[t][s]);
    }
    for (int k = 0; k <= n; k++) {
      for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
          root[i][j] = min(root[i][j], root[i][k] + root[k][j]);
        }
      }
    }
    if (ch)
      rec(0, 0, 0, 0);
    if (ac >= INF || ch == false)
      cout << "impossible" << endl;
    else
      cout << ac << " " << ad << endl;
  }
  return 0;
}