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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge {
  int to;
  int cost;
};

class Situation {
public:
  int sumCost;
  int sit;
  int sumDist;
  int node;

  Situation(int sumCost_, int node_, int sit_, int sumDist_) {
    sumCost = sumCost_;
    node = node_;
    sit = sit_;
    sumDist = sumDist_;
  }
  Situation();
  // Å¬ïpÔÉ\\[g
  bool operator<(const Situation &s) const { return s.sumDist < this->sumDist; }
};

// typedef pair<int,int> P;
int V;
const int MAX_V = 15;
vector<edge> G[MAX_V];
int d[MAX_V][1 << 15];
int minDist[MAX_V][1 << 15];

const int INF = 1000000000;
int n;
int idx;
int q;

string obj[20];
map<string, int> sellInfo[20];
map<string, int> dict;

void dijkstra2(int s) {
  priority_queue<Situation> que;
  for (int i = 0; i < MAX_V; i++) {
    fill(d[i], d[i] + (1 << 15), INF);
    fill(minDist[i], minDist[i] + (1 << 15), INF);
  }
  d[s][(1 << q) - 1] = 0;
  minDist[s][(1 << q) - 1] = 0;
  que.push(Situation(0, s, (1 << q) - 1, 0));
  while (!que.empty()) {
    Situation p = que.top();
    que.pop();
    int v = p.node;
    int sit = p.sit;
    if (d[v][sit] < p.sumCost)
      continue;
    if (d[v][sit] == p.sumCost && minDist[v][sit] < p.sumDist) {
      continue;
    }
    // Ü¾ÁÄ¢È¢¤iðæèÉs­
    for (map<string, int>::iterator it = sellInfo[v].begin();
         it != sellInfo[v].end(); it++) {
      string name = it->first;
      int value = it->second;
      // ÖWÈ¢¤i
      if (dict.find(name) == dict.end())
        continue;
      // idðmÛ
      int id = dict[name];
      // rbgªÜ¾§ÁÄ¢éÈçÎA§ÄÄL[ÉÇÁ
      if ((sit >> id) & 1) {
        int nsit = sit;
        nsit &= ~(1 << id);
        int ncost = p.sumCost + value;
        // Å¬RXgðÇÁÅ«éÈçÎ
        if (d[v][nsit] > d[v][sit] + value) {
          // XV
          d[v][nsit] = d[v][sit] + value;
          que.push(Situation(ncost, v, nsit, p.sumDist));
          minDist[v][nsit] = p.sumDist;
        } else if (d[v][nsit] == d[v][sit] + value) {
          if (minDist[v][nsit] > minDist[v][sit]) {
            minDist[v][nsit] = minDist[v][sit];
          }
        }
      }
    }
    for (int j = 0; j < G[v].size(); j++) {
      edge e = G[v][j];
      // àzªXVÅ«éÈçXV
      if (d[e.to][sit] > d[v][sit]) {
        // XV
        d[e.to][sit] = d[v][sit];
        minDist[e.to][sit] = p.sumDist + e.cost;
        que.push(Situation(p.sumCost, e.to, sit, minDist[e.to][sit]));
      }
      // Ú®æÌRXgª¯¶ê
      else if (d[e.to][sit] == d[v][sit]) {
        // Å¬RXgðXVÅ«éÈçAÚ®
        if (minDist[e.to][sit] > minDist[v][sit] + e.cost) {
          minDist[e.to][sit] = minDist[v][sit] + e.cost;
          que.push(Situation(p.sumCost, e.to, sit, minDist[e.to][sit]));
        }
      }
    }
  }
}

int main() {
  while (cin >> n && n != 0) {
    idx = 0;
    dict.clear();
    for (int i = 0; i <= n; i++) {
      G[i].clear();
      sellInfo[i].clear();
    }
    for (int i = 0; i < 20; i++)
      obj[i].clear();
    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        string name;
        int value;
        cin >> name;
        scanf("%d", &value);
        sellInfo[i + 1][name] = value;
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      string name;
      cin >> name;
      obj[i] = name;
      dict[name] = i;
    }
    int m;
    scanf("%d", &m);
    V = n;
    for (int i = 0; i < m; i++) {
      int s, t, d;
      scanf("%d %d %d", &s, &t, &d);
      edge e;
      e.to = t;
      e.cost = d;
      G[s].push_back(e);
      e.to = s;
      G[t].push_back(e);
    }
    dijkstra2(0);
    int minv = d[0][0];
    int mind = minDist[0][0];
    if (minv == INF)
      printf("impossible\\n");
    else
      printf("%d %d\\n", minv, mind);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge {
  int to;
  int cost;
};

class Situation {
public:
  int sumCost;
  int sit;
  int sumDist;
  int node;

  Situation(int sumCost_, int node_, int sit_, int sumDist_) {
    sumCost = sumCost_;
    node = node_;
    sit = sit_;
    sumDist = sumDist_;
  }
  Situation();
  // Å¬ïpÔÉ\\[g
  bool operator<(const Situation &s) const { return s.sumDist < this->sumDist; }
};

// typedef pair<int,int> P;
int V;
const int MAX_V = 15;
vector<edge> G[MAX_V];
int d[MAX_V][1 << 15];
int minDist[MAX_V][1 << 15];

const int INF = 1000000000;
int n;
int idx;
int q;

string obj[20];
map<string, int> sellInfo[20];
map<string, int> dict;

void dijkstra2(int s) {
  priority_queue<Situation> que;
  for (int i = 0; i < MAX_V; i++) {
    fill(d[i], d[i] + (1 << 15), INF);
    fill(minDist[i], minDist[i] + (1 << 15), INF);
  }
  d[s][(1 << q) - 1] = 0;
  minDist[s][(1 << q) - 1] = 0;
  que.push(Situation(0, s, (1 << q) - 1, 0));
  while (!que.empty()) {
    Situation p = que.top();
    que.pop();
    int v = p.node;
    int sit = p.sit;
    if (d[v][sit] < p.sumCost)
      continue;
    // Ü¾ÁÄ¢È¢¤iðæèÉs­
    for (map<string, int>::iterator it = sellInfo[v].begin();
         it != sellInfo[v].end(); it++) {
      string name = it->first;
      int value = it->second;
      // ÖWÈ¢¤i
      if (dict.find(name) == dict.end())
        continue;
      // idðmÛ
      int id = dict[name];
      // rbgªÜ¾§ÁÄ¢éÈçÎA§ÄÄL[ÉÇÁ
      if ((sit >> id) & 1) {
        int nsit = sit;
        nsit &= ~(1 << id);
        int ncost = p.sumCost + value;
        // Å¬RXgðÇÁÅ«éÈçÎ
        if (d[v][nsit] > d[v][sit] + value) {
          // XV
          d[v][nsit] = d[v][sit] + value;
          que.push(Situation(ncost, v, nsit, p.sumDist));
          minDist[v][nsit] = p.sumDist;
        } else if (d[v][nsit] == d[v][sit] + value) {
          if (minDist[v][nsit] > minDist[v][sit]) {
            minDist[v][nsit] = minDist[v][sit];
          }
        }
      }
    }
    for (int j = 0; j < G[v].size(); j++) {
      edge e = G[v][j];
      // àzªXVÅ«éÈçXV
      if (d[e.to][sit] > d[v][sit]) {
        // XV
        d[e.to][sit] = d[v][sit];
        minDist[e.to][sit] = p.sumDist + e.cost;
        que.push(Situation(p.sumCost, e.to, sit, minDist[e.to][sit]));
      }
      // Ú®æÌRXgª¯¶ê
      else if (d[e.to][sit] == d[v][sit]) {
        // Å¬RXgðXVÅ«éÈçAÚ®
        if (minDist[e.to][sit] > minDist[v][sit] + e.cost) {
          minDist[e.to][sit] = minDist[v][sit] + e.cost;
          que.push(Situation(p.sumCost, e.to, sit, minDist[e.to][sit]));
        }
      }
    }
  }
}

int main() {
  while (cin >> n && n != 0) {
    idx = 0;
    dict.clear();
    for (int i = 0; i <= n; i++) {
      G[i].clear();
      sellInfo[i].clear();
    }
    for (int i = 0; i < 20; i++)
      obj[i].clear();
    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      // cin>>k;
      for (int j = 0; j < k; j++) {
        string name;
        int value;
        cin >> name;
        scanf("%d", &value);
        sellInfo[i + 1][name] = value;
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      string name;
      cin >> name;
      obj[i] = name;
      dict[name] = i;
    }
    int m;
    scanf("%d", &m);
    //   cin>>m;
    V = n;
    for (int i = 0; i < m; i++) {
      int s, t, d;
      scanf("%d %d %d", &s, &t, &d);
      // cin>>s>>t>>d;
      edge e;
      e.to = t;
      e.cost = d;
      G[s].push_back(e);
      e.to = s;
      G[t].push_back(e);
    }
    dijkstra2(0);
    int minv = d[0][0];
    int mind = minDist[0][0];
    if (minv == INF)
      printf("impossible\\n");
    // cout<<"impossible"<<endl;
    else
      printf("%d %d\\n", minv, mind);
    // cout<<minv<<" "<<mind<<endl;
  }

  return 0;
}