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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cost;
  Edge(int to0, int cost0) {
    to = to0;
    cost = cost0;
  }
};

const int INF = INT_MAX / 2;

void solve(vector<vector<string>> &name, vector<vector<int>> &price,
           vector<string> &need, vector<vector<Edge>> &edges) {
  int n = name.size();
  int q = need.size();

  vector<int> minPrice(q, INF);
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      for (unsigned k = 0; k < name[j].size(); ++k) {
        if (name[j][k] == need[i] && price[j][k] < minPrice[i])
          minPrice[i] = price[j][k];
      }
    }
  }
  vector<bitset<15>> buy(n, 0);
  for (int i = 0; i < q; ++i) {
    if (minPrice[i] == INF) {
      cout << "impossible" << endl;
      return;
    }
    for (int j = 0; j < n; ++j) {
      for (unsigned k = 0; k < name[j].size(); ++k) {
        if (name[j][k] == need[i] && price[j][k] == minPrice[i])
          buy[j][i] = true;
      }
    }
  }
  cout << accumulate(minPrice.begin(), minPrice.end(), 0) << ' ';

  vector<vector<int>> dp(n, vector<int>(1 << q, INF));
  multimap<int, pair<int, int>> m;
  dp[0][0] = 0;
  m.insert(make_pair(0, make_pair(0, 0)));
  for (;;) {
    multimap<int, pair<int, int>>::iterator it = m.begin();
    int cost = it->first;
    int pos = it->second.first;
    bitset<15> bs(it->second.second);
    m.erase(it);
    if (cost > dp[pos][bs.to_ulong()])
      continue;
    if (pos == 0 && bs.count() == q) {
      cout << cost << endl;
      return;
    }
    for (unsigned i = 0; i < edges[pos].size(); ++i) {
      int to = edges[pos][i].to;
      int cost1 = cost + edges[pos][i].cost;
      bitset<15> bs1 = bs | buy[to];
      if (cost1 < dp[to][bs1.to_ulong()]) {
        dp[to][bs1.to_ulong()] = cost1;
        m.insert(make_pair(cost1, make_pair(to, bs1.to_ulong())));
      }
    }
  }
}

int main() {
  for (;;) {
    int n, q, m;
    cin >> n;
    if (n == 0)
      return 0;

    vector<vector<string>> name(n + 1);
    vector<vector<int>> price(n + 1);
    for (int i = 1; i <= n; ++i) {
      int k;
      cin >> k;
      name[i].resize(k);
      price[i].resize(k);
      for (int j = 0; j < k; ++j)
        cin >> name[i][j] >> price[i][j];
    }

    cin >> q;
    vector<string> need(q);
    for (int i = 0; i < q; ++i)
      cin >> need[i];

    cin >> m;
    vector<vector<Edge>> edges(n + 1);
    for (int i = 0; i < m; ++i) {
      int s, t, d;
      cin >> s >> t >> d;
      edges[s].push_back(Edge(t, d));
      edges[t].push_back(Edge(s, d));
    }

    solve(name, price, need, edges);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cost;
  Edge(int to0, int cost0) {
    to = to0;
    cost = cost0;
  }
};

const int INF = INT_MAX / 2;

void solve(vector<vector<string>> &name, vector<vector<int>> &price,
           vector<string> &need, vector<vector<Edge>> &edges) {
  int n = name.size();
  int q = need.size();

  vector<int> minPrice(q, INF);
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      for (unsigned k = 0; k < name[j].size(); ++k) {
        if (name[j][k] == need[i] && price[j][k] < minPrice[i])
          minPrice[i] = price[j][k];
      }
    }
  }
  vector<bitset<15>> buy(n, 0);
  for (int i = 0; i < q; ++i) {
    if (minPrice[i] == INF) {
      cout << "impossible" << endl;
      return;
    }
    for (int j = 0; j < n; ++j) {
      for (unsigned k = 0; k < name[j].size(); ++k) {
        if (name[j][k] == need[i] && price[j][k] == minPrice[i])
          buy[j][i] = true;
      }
    }
  }
  cout << accumulate(minPrice.begin(), minPrice.end(), 0) << ' ';

  vector<vector<int>> dp(n, vector<int>(1 << q, INF));
  map<int, pair<int, int>> m;
  dp[0][0] = 0;
  m.insert(make_pair(0, make_pair(0, 0)));
  for (;;) {
    map<int, pair<int, int>>::iterator it = m.begin();
    int cost = it->first;
    int pos = it->second.first;
    bitset<15> bs(it->second.second);
    m.erase(it);
    if (cost > dp[pos][bs.to_ulong()])
      continue;
    if (pos == 0 && bs.count() == q) {
      cout << cost << endl;
      return;
    }
    for (unsigned i = 0; i < edges[pos].size(); ++i) {
      int to = edges[pos][i].to;
      int cost1 = cost + edges[pos][i].cost;
      bitset<15> bs1 = bs | buy[to];
      if (cost1 < dp[to][bs1.to_ulong()]) {
        dp[to][bs1.to_ulong()] = cost1;
        m.insert(make_pair(cost1, make_pair(to, bs1.to_ulong())));
      }
    }
  }
}

int main() {
  for (;;) {
    int n, q, m;
    cin >> n;
    if (n == 0)
      return 0;

    vector<vector<string>> name(n + 1);
    vector<vector<int>> price(n + 1);
    for (int i = 1; i <= n; ++i) {
      int k;
      cin >> k;
      name[i].resize(k);
      price[i].resize(k);
      for (int j = 0; j < k; ++j)
        cin >> name[i][j] >> price[i][j];
    }

    cin >> q;
    vector<string> need(q);
    for (int i = 0; i < q; ++i)
      cin >> need[i];

    cin >> m;
    vector<vector<Edge>> edges(n + 1);
    for (int i = 0; i < m; ++i) {
      int s, t, d;
      cin >> s >> t >> d;
      edges[s].push_back(Edge(t, d));
      edges[t].push_back(Edge(s, d));
    }

    solve(name, price, need, edges);
  }
}