Language: C++

#include <iostream>
#include <tuple>
#include <vector>
#include <queue>

char stage[20][20];
int cost[1024][10];
int dis[10][10];
int w, h;

typedef std::tuple<int, int, int> T;
std::vector<T> pos_to_num;

long long unsigned PopCount(long long unsigned x) {
	x = (x & 0x5555555555555555ULL) + ((x & 0xAAAAAAAAAAAAAAAAULL) >> 1);
	x = (x & 0x3333333333333333ULL) + ((x & 0xCCCCCCCCCCCCCCCCULL) >> 2);
	x = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x & 0xF0F0F0F0F0F0F0F0ULL) >> 4);

	x *= 0x0101010101010101ULL;

	return x;
}

void dijkstra(int sx, int sy)
{
	int c[20][20] = {0};
	int f[20][20] = {0};
	int dx[4] = {1, -1, 0, 0};
	int dy[4] = {0, 0, 1, -1};

	std::queue<std::tuple<int,int>> q;
	q.push(std::make_tuple(sx, sy));
	f[sx][sy] = 1;

	while(!q.empty()) {
		auto a = q.front(); q.pop();
		int x = std::get<0>(a);
		int y = std::get<1>(a);
		for ( int i = 0; i < 4; ++i ) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if ( nx < 0 || nx >= w ) continue;
			if ( ny < 0 || ny >= h ) continue;
			if ( stage[nx][ny] == 'x' ) continue;
			if ( f[nx][ny] == 1 ) continue;

			c[nx][ny] = c[x][y] + 1;
			f[nx][ny] = 1;
			q.push(std::make_tuple(nx, ny));
		}
	}

	if ( stage[sx][sy] == 'o' ) {
		for ( int i = 0; i < pos_to_num.size(); ++i ) {
			cost[0][std::get<2>(pos_to_num[i])] = c[std::get<0>(pos_to_num[i])][std::get<1>(pos_to_num[i])];
		}
	} else if ( stage[sx][sy] == '*' ) {
		int num;
		for ( int i = 0; i < pos_to_num.size(); ++i ) {
			if ( sx == std::get<0>(pos_to_num[i]) && sy == std::get<1>(pos_to_num[i]) ) {
				num = std::get<2>(pos_to_num[i]);
			}
		}
		for ( int i = 0; i < pos_to_num.size(); ++i ) {
			dis[num][std::get<2>(pos_to_num[i])] = c[std::get<0>(pos_to_num[i])][std::get<1>(pos_to_num[i])];
		}
	}
}

int main()
{
	while ( true ) {
		for ( int i = 0; i < 1024; ++i ) {
			for ( int j = 0; j < 10; ++j ) {
				cost[i][j] = 0xfffff;
			}
		}

		std::cin >> w >> h;
		if ( w == 0 && h == 0 ) return 0;
 
		int sx, sy;
		int k = 0;
		for ( int i = 0; i < h; ++i ) {
			for ( int j = 0; j < w; ++j ) {
				std::cin >> stage[j][i];
				if ( stage[j][i] == '*' ) {
					pos_to_num.push_back(T(j, i, k));
					k++;
				}
				if ( stage[j][i] == 'o' ) {
					sx = j; sy = i;
				}
			}
		}

		dijkstra(sx, sy);
		for ( auto &v: pos_to_num ) {
			dijkstra(std::get<0>(v), std::get<1>(v));
		}
		std::vector<std::tuple<int, int>> table(1<<k);
		for ( int i = 0; i < (1<<k); ++i ) {
			table[i] = std::make_tuple(PopCount(i), i);
		}
		std::sort(table.begin(), table.end());

		for ( int i = 1; i < (1<<k); ++i ) {
			int q = std::get<1>(table[i]);
			for ( int j = 0; j < k; ++j ) {
				if ( q & (1<<j) ) {
					for ( int l = 0; l < k; ++l ) {
						int c = cost[q^(1<<j)][j] + dis[j][l];
						cost[q][l] = (c < cost[q][l]) ? c : cost[q][l];
						//std::cout << "state:" << q << " to:" << l << " cost:" << cost[q][l] << std::endl;
					}
				}
			}
		}
		int min = 0xfffff;
		for ( int i = 0; i < k; ++i ) {
			if ( min > cost[(1<<k)-1][i] ) min = cost[(1<<k)-1][i];
		}
		if ( min == 0xfffff || min == 0 ) {
			std::cout << -1 << std::endl;
		} else {
			std::cout << min << std::endl;
		}
	}


	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:105:22: error: 'sort' is not a member of 'std'; did you mean 'qsort'?
  105 |                 std::sort(table.begin(), table.end());
      |                      ^~~~
      |                      qsort