Problem p00719 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Route {
  int x, y, z;
};

struct Path {
  int mask;
  int city;
  vector<int> pathCost;
};

bool solve() {
  int n, m, p, a, b;
  cin >> n >> m >> p >> a >> b;

  if ((n | m | p | a | b) == 0)
    return false;

  //?????????????????????????????????
  a--;
  b--;

  vector<int> t(n);

  for (auto &_t : t) {
    cin >> _t;
  }

  // dummy data
  t.push_back(-10);

  sort(t.begin(), t.end(), std::greater<int>());

  vector<Route> route(p);
  vector<Route> bin[30];

  for (auto &r : route) {
    cin >> r.x;
    cin >> r.y;
    cin >> r.z;

    //?????????????????????????????????
    r.x--;
    r.y--;

    bin[r.x].push_back(r);
    bin[r.y].push_back(Route{r.y, r.x, r.z});
  }

  for (int i = 0; i < 30; i++) {
    //        if(bin[i].size())cout << i << " : " << bin[i].size() << endl;
  }

  // a???start
  // b???goal
  queue<Path> que;
  queue<Path> ansPath;

  Path tmp{1 << a, a};
  tmp.pathCost.push_back(0);

  que.push(tmp);

  double minCost[30] = {};
  for (auto &mc : minCost) {
    mc = 1 << 30;
  }
  minCost[a] = 0.0;

  // n?????¬??????????????°
  for (int i = 0; i < n; i++) {
    while (que.front().pathCost.size() == (i + 1)) {
      // que.front???????????¶???
      int nowCity = que.front().city;
      // cout << "now : " << nowCity << " loops " << bin[nowCity].size() <<
      // "times\\n";
      for (int j = 0; j < bin[nowCity].size(); j++) {
        //?????§?????\\??????????????????????????????
        if (que.front().mask & 1 << bin[nowCity][j].y) {
          // cout << bin[nowCity][j].y << " skip\\n";
          continue;
        }
        //????????????????§??????????
        Path next = que.front();
        next.mask |= 1 << bin[nowCity][j].y;
        next.city = bin[nowCity][j].y;
        next.pathCost.push_back(bin[nowCity][j].z);

        //?????£???????????????????????§???????????°?????????
        sort(next.pathCost.begin(), next.pathCost.end(), std::greater<int>());

        double cost_tmp = 0.0;
        for (int i = 0; i < next.pathCost.size(); i++) {
          // cout << cost[i] << " " << t[i] << endl;
          cost_tmp += (double)next.pathCost[i] / (double)t[i];
        }

        if (cost_tmp < minCost[next.city])
          minCost[next.city] = cost_tmp;
        else
          continue;

        // cout << " next : " << next.city << endl;

        //???????????????????????????
        if (next.city == b)
          ansPath.push(next);
        else
          que.push(next);
      }

      que.pop();
      // cout << " : " << que.size() << " " << ansPath.size() << endl;
    }
    //        cout << "Yeah!\\n";
  }

  if (ansPath.size() == 0) {
    cout << "Impossible\\n";
    return true;
  }

  double ans_min = double(1 << 30);

  while (!ansPath.empty()) {
    Path q = ansPath.front();
    ansPath.pop();

    //       cout << q.mask << endl;

    vector<int> cost = q.pathCost;

    sort(cost.begin(), cost.end(), std::greater<int>());

    double ans = 0.0;
    for (int i = 0; i < cost.size(); i++) {
      //            cout << cost[i] << " " << t[i] << endl;
      ans += (double)cost[i] / (double)t[i];
    }

    if (ans < ans_min)
      ans_min = ans;
    //        cout << endl;
  }

  cout << ans_min << endl;

  return true;
}

int main() {
  while (solve())
    ;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Route {
  int x, y, z;
};

struct Path {
  int mask;
  int city;
  vector<int> pathCost;
};

bool solve() {
  int n, m, p, a, b;
  cin >> n >> m >> p >> a >> b;

  if ((n | m | p | a | b) == 0)
    return false;

  //?????????????????????????????????
  a--;
  b--;

  vector<int> t(n);

  for (auto &_t : t) {
    cin >> _t;
  }

  // dummy data
  t.push_back(-10);

  sort(t.begin(), t.end(), std::greater<int>());

  vector<Route> route(p);
  vector<Route> bin[30];

  for (auto &r : route) {
    cin >> r.x;
    cin >> r.y;
    cin >> r.z;

    //?????????????????????????????????
    r.x--;
    r.y--;

    bin[r.x].push_back(r);
    bin[r.y].push_back(Route{r.y, r.x, r.z});
  }

  for (int i = 0; i < 30; i++) {
    //        if(bin[i].size())cout << i << " : " << bin[i].size() << endl;
  }

  // a???start
  // b???goal
  queue<Path> que;
  queue<Path> ansPath;

  Path tmp{1 << a, a};
  tmp.pathCost.push_back(0);

  que.push(tmp);

  // n?????¬??????????????°
  for (int i = 0; i < n; i++) {
    while (que.front().pathCost.size() == (i + 1)) {
      // que.front???????????¶???
      int nowCity = que.front().city;
      // cout << "now : " << nowCity << " loops " << bin[nowCity].size() <<
      // "times\\n";
      for (int j = 0; j < bin[nowCity].size(); j++) {
        //?????§?????\\??????????????????????????????
        if (que.front().mask & 1 << bin[nowCity][j].y) {
          // cout << bin[nowCity][j].y << " skip\\n";
          continue;
        }
        //????????????????§??????????
        Path next = que.front();
        next.mask |= 1 << bin[nowCity][j].y;
        next.city = bin[nowCity][j].y;
        next.pathCost.push_back(bin[nowCity][j].z);

        // cout << " next : " << next.city << endl;

        //???????????????????????????
        if (next.city == b)
          ansPath.push(next);
        else
          que.push(next);
      }

      que.pop();
      // cout << " : " << que.size() << " " << ansPath.size() << endl;
    }
  }

  if (ansPath.size() == 0) {
    cout << "Impossible\\n";
    return true;
  }

  double ans_min = double(1 << 30);

  while (!ansPath.empty()) {
    Path q = ansPath.front();
    ansPath.pop();

    //       cout << q.mask << endl;

    vector<int> cost = q.pathCost;

    sort(cost.begin(), cost.end(), std::greater<int>());

    double ans = 0.0;
    for (int i = 0; i < cost.size(); i++) {
      //            cout << cost[i] << " " << t[i] << endl;
      ans += (double)cost[i] / (double)t[i];
    }

    if (ans < ans_min)
      ans_min = ans;
    //        cout << endl;
  }

  cout << ans_min << endl;

  return true;
}

int main() {
  while (solve())
    ;

  return 0;
}