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

#include <bits/stdc++.h>
#define int long long
using namespace std;

template <class T> struct Edge {
  int from, to;
  T cost;
  Edge(int to, T cost) : to(to), cost(cost) {}
  Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
  operator int() const noexcept { return to; }
};

template <class T> using WeightedGraph = vector<vector<Edge<T>>>;
using Graph = vector<vector<int>>;
template <class T> using Matrix = vector<vector<T>>;

template <class T> void warshallFloyd(Matrix<T> &G) {
  for (int k = 0; k < G.size(); k++) {
    for (int i = 0; i < G.size(); i++) {
      for (int j = 0; j < G.size(); j++) {
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
      }
    }
  }
}

int w, h;

inline int ind(int x, int y) { return x * w + y; }

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void solve() {
  const int INF = 1e9;
  string field[25];
  for (int i = 0; i < h; i++)
    cin >> field[i];
  int pos[20], cnt = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'o')
        pos[0] = ind(i, j);
      else if (field[i][j] == '*')
        pos[cnt++] = ind(i, j);
    }
  }
  Matrix<int> G(h * w, vector<int>(h * w, INF));
  for (int i = 0; i < h * w; i++)
    G[i][i] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] != 'x') {
          G[ind(i, j)][ind(nx, ny)] = 1;
        }
      }
    }
  }
  warshallFloyd(G);
  vector<int> vec(cnt);
  iota(vec.begin(), vec.end(), 0);
  int ans = INF;
  do {
    int sum = 0;
    for (int i = 1; i < cnt; i++)
      sum += G[pos[vec[i - 1]]][pos[vec[i]]];
    ans = min(ans, sum);
  } while (next_permutation(vec.begin() + 1, vec.end()));
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

signed main() {
  while (cin >> w >> h, w)
    solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define INF 1e+9
using namespace std;

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

typedef pair<int, int> P;

int main() {
  int h, w;
  while (cin >> w >> h, h) {
    int dirtx[10], dirty[10], sx, sy, cnt = 0, mi = INF;
    vector<int> perm;
    string field[20];
    for (int i = 0; i < h; i++) {
      cin >> field[i];
      for (int j = 0; j < w; j++) {
        if (field[i][j] == 'o') {
          sx = i;
          sy = j;
        } else if (field[i][j] == '*') {
          dirtx[cnt] = i;
          dirty[cnt] = j;
          perm.push_back(cnt++);
        }
      }
    }
    do {
      int tx = sx, ty = sy, ans = 0, d[20][20];
      for (int nxt : perm) {
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++)
            d[i][j] = INF;
        }
        queue<P> que;
        que.push(P(tx, ty));
        d[tx][ty] = 0;
        while (!que.empty()) {
          P p = que.front();
          que.pop();
          int x = p.first, y = p.second;
          for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
                field[nx][ny] != 'x' && d[nx][ny] == INF) {
              d[nx][ny] = d[x][y] + 1;
              que.push(P(nx, ny));
            }
          }
        }
        ans += d[dirtx[nxt]][dirty[nxt]];
        tx = dirtx[nxt];
        ty = dirty[nxt];
      }
      mi = min(mi, ans);
    } while (next_permutation(perm.begin(), perm.end()));
    if (mi != INF)
      cout << mi << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}