alexjercan/bugnet huggingface dataset:
Problem p01473 (C++) — buggy submission, verdict: Runtime Error

/*
 * 2356.cc: Palindromic Anagram
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 20;

/* typedef */

typedef long long ll;

/* global variables */

int as[26];
ll fracs[MAX_N + 1];

/* subroutines */

inline ll nck(int n, int k) { return fracs[n] / fracs[n - k] / fracs[k]; }

void prepare_fracs(int n) {
  fracs[0] = 1;
  for (int i = 1; i <= n; i++)
    fracs[i] = fracs[i - 1] * i;
}

/* main */

int main() {
  string s;
  cin >> s;
  int sn = s.size(), n = sn / 2;

  for (int i = 0; i < sn; i++)
    as[s[i] - 'a']++;

  int oddn = 0;
  for (int i = 0; i < 26; i++) {
    if (as[i] & 1)
      oddn++;
    as[i] /= 2;
  }
  if (oddn > 1) {
    puts("0");
    return 0;
  }

  prepare_fracs(n);

  ll ans = 1;
  int k = n;

  for (int i = 0; i < 26; i++) {
    // printf("%d: nck(%d,%d)\\n", i, k, as[i]);
    ans *= nck(k, as[i]);
    k -= as[i];
  }

  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

/*
 * 2356.cc: Palindromic Anagram
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 20;

/* typedef */

typedef long long ll;

/* global variables */

int as[26];
ll fracs[MAX_N + 1];

/* subroutines */

inline ll nck(int n, int k) { // nCk % MOD
  return fracs[n] / fracs[n - k] / fracs[k];
}

void prepare_fracs(int n) {
  fracs[0] = 1;
  for (int i = 1; i <= n; i++)
    fracs[i] = fracs[i - 1] * i;
}

/* main */

int main() {
  string s;
  cin >> s;
  int sn = s.size(), n = sn / 2;

  for (int i = 0; i < sn; i++)
    as[s[i] - 'a']++;

  int oddn = 0;
  for (int i = 0; i < sn; i++) {
    if (as[i] & 1)
      oddn++;
    as[i] /= 2;
  }
  if (oddn > 1) {
    puts("0");
    return 0;
  }

  prepare_fracs(n);

  ll ans = 1;
  int k = n;

  for (int i = 0; i < 26; i++) {
    ans *= nck(k, as[i]);
    k -= as[i];
  }

  printf("%lld\\n", ans);
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)

int n;
int cost[8][8];

inline int encode(const vector<int> &v) {
  int ret = 0;
  for (int i = 0; i < v.size(); ++i)
    ret |= (v[i] << (i * 3));
  return ret;
}

inline int get(const int x, int idx) { return 7 & (x >> (idx * 3)); }

int solve() {
  map<int, int> dist;
  set<int> seen;
  vector<int> s(n);
  for (int i = 0; i < n; ++i)
    s[i] = i;
  dist[encode(s)] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, encode(s)));

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int d = p.first, st = p.second;

    if (seen.find(st) != seen.end())
      continue;
    seen.insert(st);

    for (int i = 0; i < n; ++i)
      s[i] = get(st, i);
    // for(int i=0; i<n; ++i) cout<<s[i]<<" ";
    // cout<<endl;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        swap(s[i], s[j]);
        int nd = d + cost[s[i]][s[j]];
        int ns = encode(s);
        if (dist.find(ns) == dist.end() || dist[ns] > nd) {
          dist[ns] = nd;
          q.push(make_pair(nd, ns));
        }
        swap(s[i], s[j]);
      }
  }

  int ans = 0;
  for (map<int, int>::iterator it = dist.begin(); it != dist.end(); ++it)
    ans = max(ans, it->second);

  //    cout<<dist.size()<<endl;
  return ans;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> cost[i][j];
  cout << solve() << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)

int n;
int cost[8][8];

inline int encode(const vector<int> &v) {
  int ret = 0;
  for (int i = 0; i < v.size(); ++i)
    ret |= (v[i] << (i * 3));
  return ret;
}

inline int get(const int x, int idx) { return 7 & (x >> (idx * 3)); }

int solve() {
  map<int, int> dist;
  vector<int> s(n);
  for (int i = 0; i < n; ++i)
    s[i] = i + 1;
  dist[encode(s)] = 0;
  priority_queue<pair<int, int>> q;
  q.push(make_pair(0, encode(s)));

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();

    int d = p.first, st = p.second;
    for (int i = 0; i < n; ++i)
      s[i] = get(st, i);

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        int nd = d + cost[s[i] - 1][s[j] - 1];
        swap(s[i], s[j]);
        int ns = encode(s);
        if (dist.find(ns) == dist.end() || dist[ns] > nd) {
          dist[ns] = nd;
          q.push(make_pair(nd, ns));
        }
        swap(s[i], s[j]);
      }
  }

  int ans = 0;
  for (map<int, int>::iterator it = dist.begin(); it != dist.end(); ++it)
    ans = max(ans, it->second);
  return ans;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> cost[i][j];
  cout << solve() << endl;
}
Problem p01478 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
typedef pair<int, int> ii;

void show(int x, int n) {
  cout << "(";
  rep(i, n) {
    cout << (x - (x / 8) * 8) << ",";
    x >>= 3;
  }
  cout << ") ";
}

int dijk(int n, const vector<vector<int>> &c) {
  int start = 0;
  rep(i, n) start = (start << 3) + i;
  vector<bool> cost(16777216, false);
  priority_queue<pair<int, int>> pq;
  pq.push(ii(0, start));

  int r = -1;

  while (!pq.empty()) {
    int cur = pq.top().second;
    int dist = pq.top().first;
    pq.pop();
    if (cost[cur])
      continue;
    // show(cur,n); cout << dist << endl;
    cost[cur] = true;
    r = max(r, -dist);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int x, y;
        x = (cur >> (i * 3)) & 7, y = (cur >> (j * 3)) & 7;
        cur = cur - (x << (i * 3)) - (y << (j * 3)) + (x << (j * 3)) +
              (y << (i * 3));
        if (!cost[cur])
          pq.push(ii(dist - c[i][j], cur));
        x = (cur >> (i * 3)) & 7, y = (cur >> (j * 3)) & 7;
        cur = cur - (x << (i * 3)) - (y << (j * 3)) + (x << (j * 3)) +
              (y << (i * 3));
      }
    }
  }

  return r;
}

int main() {
  int n;

  while (cin >> n) {
    vector<vector<int>> c(n, vector<int>(n));
    rep(i, n) rep(j, n) cin >> c[i][j];
    cout << dijk(n, c) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()

struct Data {
  vector<int> s;
  int c;
  Data() {}
  Data(vector<int> _s, int _c) { s = _s, c = _c; }
  bool operator<(const Data &o) const { return c > o.c; }
};

int f(vector<int> v) {
  int r = 0;
  rep(i, v.size()) r = v[i] + r * 8;
  return r;
}

vector<int> dijk(int n, const vector<vector<int>> &c) {
  vector<int> start(n);
  rep(i, n) start[i] = i;
  // map<vector<int>, int> cost;
  vector<int> cost(20000000, -1);
  priority_queue<Data> pq;
  pq.push(Data(start, 0));

  while (!pq.empty()) {
    vector<int> cur = pq.top().s;
    int dist = pq.top().c;
    pq.pop();
    if (cost[f(cur)] != -1)
      continue;
    cost[f(cur)] = dist;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        swap(cur[i], cur[j]);
        pq.push(Data(cur, dist + c[i][j]));
        swap(cur[i], cur[j]);
      }
    }
  }

  return cost;
}

int main() {
  int n;

  while (cin >> n) {
    vector<vector<int>> c(n, vector<int>(n));
    rep(i, n) rep(j, n) cin >> c[i][j];
    vector<int> cost = dijk(n, c);
    int ans = -1;
    /*
    for(map<vector<int>, int>::iterator it=cost.begin(); it!=cost.end(); it++) {
            //cout << "("; rep(i, n) cout << (it->first)[i] << ","; cout << ") "
    << it->second << endl; ans = max(ans, it->second);
    }
    */
    rep(i, 20000000) if (cost[i] != -1) ans = max(ans, cost[i]);
    cout << ans << endl;
  }

  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) REP(i, 0, n)

#define INF (1e9)

int main() {

  int N;
  cin >> N;
  vector<vector<int>> v(N, vector<int>(N));
  rep(i, N) rep(j, N) { cin >> v[i][j]; }

  vector<int> V(N);
  iota(V.begin(), V.end(), 0);

  priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                 greater<pair<int, vector<int>>>>
      q;
  map<vector<int>, int> mp;
  mp[V] = 0;
  q.emplace(0, V);
  while (!q.empty()) {
    int co;
    vector<int> V;
    tie(co, V) = q.top();
    q.pop();

    rep(i, N) REP(j, i + 1, N) {
      swap(V[i], V[j]);
      if (mp.find(V) != mp.end() && mp[V] <= co + v[V[i]][V[j]]) {
        swap(V[i], V[j]);
        continue;
      }

      mp[V] = co + v[V[i]][V[j]];
      q.emplace(co + v[V[i]][V[j]], V);
      swap(V[i], V[j]);
    }
  }

  int ans = 0;

  for (auto &&e : mp) {
    ans = max(ans, e.second);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) REP(i, 0, n)

#define INF (1e9)

int main() {

  int N;
  cin >> N;
  vector<vector<int>> v(N, vector<int>(N));
  rep(i, N) rep(j, N) { cin >> v[i][j]; }

  vector<int> V(N);
  iota(V.begin(), V.end(), 0);

  queue<pair<int, vector<int>>> q;
  map<vector<int>, int> mp;
  mp[V] = 0;
  q.emplace(0, V);
  while (!q.empty()) {
    int co;
    vector<int> V;
    tie(co, V) = q.front();
    q.pop();

    rep(i, N) REP(j, i + 1, N) {
      swap(V[i], V[j]);
      if (mp.find(V) != mp.end() && mp[V] <= co + v[V[i]][V[j]]) {
        swap(V[i], V[j]);
        continue;
      }

      mp[V] = co + v[V[i]][V[j]];
      q.emplace(co + v[V[i]][V[j]], V);
      swap(V[i], V[j]);
    }
  }

  int ans = 0;

  for (auto &&e : mp) {
    ans = max(ans, e.second);
  }

  cout << ans << endl;

  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

struct S {
  int t;
  vector<int> v;
  bool operator<(S a) const { return t > a.t; }
};

int fact(int x) { return (x == 1) ? 1 : x * fact(x - 1); }

int dec(vector<int> v) {
  vector<int> n(v.size());
  for (int i = 0; i < n.size(); i++) {
    n[i] = i;
  }
  int r = 0;
  for (int i = 0; i < v.size(); i++) {
    r *= n.size();
    vector<int>::iterator it = find(n.begin(), n.end(), v[i]);
    r += it - n.begin();
    n.erase(it);
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  int cs[8][8];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cs[i][j];
    }
  }
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = i;
  }
  bool p[50000] = {};
  int r = fact(n);
  S is = {0, v};
  priority_queue<S> que;
  que.push(is);
  int lt;
  while (r) {
    S c = que.top();
    que.pop();
    if (p[dec(c.v)]++)
      continue;
    r--;
    lt = c.t;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        S n = c;
        n.t += cs[i][j];
        swap(n.v[i], n.v[j]);
        if (!p[dec(n.v)])
          que.push(n);
      }
    }
  }
  cout << lt << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

struct S {
  int t;
  vector<int> v;
  bool operator<(S a) const { return t > a.t; }
};

int fact(int x) { return (x == 1) ? 1 : x * fact(x - 1); }

int dec(vector<int> v) {
  vector<int> n(v.size());
  for (int i = 0; i < n.size(); i++) {
    n[i] = i;
  }
  int r = 0;
  for (int i = 0; i < v.size(); i++) {
    r *= n.size();
    vector<int>::iterator it = find(n.begin(), n.end(), v[i]);
    r += it - n.begin();
    n.erase(it);
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  int cs[8][8];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cs[i][j];
    }
  }
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = i;
  }
  bool p[50000] = {};
  int r = fact(n);
  S is = {0, v};
  priority_queue<S> que;
  que.push(is);
  int lt;
  while (r) {
    S c = que.top();
    que.pop();
    if (p[dec(c.v)]++)
      continue;
    r--;
    lt = c.t;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        S n = c;
        n.t += cs[i][j];
        swap(n.v[i], n.v[j]);
        que.push(n);
      }
    }
  }
  cout << lt << endl;
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n, c[8][8];
vector<int> p;
map<vector<char>, int> m;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &c[i][j]);
    }
  }
  vector<char> s;
  for (int i = 0; i < n; i++)
    s.push_back((char)(i));
  priority_queue<pair<int, vector<char>>> que;
  que.push(make_pair(-1, s));
  while (!que.empty()) {
    pair<int, vector<char>> state = que.top();
    que.pop();
    vector<char> v = state.second;
    if (m[v] != 0)
      continue;
    m[v] = state.first;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        swap(v[i], v[j]);
        que.push(make_pair(state.first - c[i][j], v));
        swap(v[i], v[j]);
      }
    }
  }
  int ret = 0;
  for (map<vector<char>, int>::iterator i = m.begin(); i != m.end(); i++) {
    ret = max(ret, -(*i).second - 1);
  }
  printf("%d\\n", ret);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n, c[8][8];
vector<int> p;
map<vector<int>, int> m;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &c[i][j]);
    }
  }
  vector<int> s;
  for (int i = 0; i < n; i++)
    s.push_back(i);
  priority_queue<pair<int, vector<int>>> que;
  que.push(make_pair(-1, s));
  while (!que.empty()) {
    pair<int, vector<int>> state = que.top();
    que.pop();
    vector<int> v = state.second;
    if (m[v] != 0)
      continue;
    m[v] = state.first;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        swap(v[i], v[j]);
        que.push(make_pair(state.first - c[i][j], v));
        swap(v[i], v[j]);
      }
    }
  }
  int ret = 0;
  for (map<vector<int>, int>::iterator i = m.begin(); i != m.end(); i++) {
    ret = max(ret, -(*i).second - 1);
  }
  printf("%d\\n", ret);
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
int dp[16434825], x[8][8];
queue<int> Q;
int main() {
  int n;
  cin >> n;
  int minp = 0, maxp = 0;
  for (int i = 0; i < n; i++) {
    minp += (n - 1 - i) * (1 << (3 * i));
    maxp += i * (1 << (3 * i));
  }
  for (int i = minp; i <= maxp; i++)
    dp[i] = 1000000007;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  int G = 0, maxn = 0;
  for (int i = 0; i < n; i++) {
    G += (1 << (3 * i)) * i;
  }
  Q.push(G);
  dp[G] = 0;
  while (!Q.empty()) {
    int H = Q.front();
    Q.pop();
    int I[8], J[8];
    for (int i = 0; i < 8; i++)
      I[i] = (H / (1 << (3 * i))) % 8;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 8; k++)
          J[k] = I[k];
        swap(J[i], J[j]);
        int K = 0;
        for (int k = 0; k < 8; k++)
          K += (1 << (3 * k)) * J[k];
        if (dp[K] > dp[H] + x[i][j]) {
          dp[K] = dp[H] + x[i][j];
          Q.push(K);
        }
      }
    }
  }
  for (int i = minp; i <= maxp; i++) {
    if (dp[i] != 1000000007 && maxn < dp[i])
      maxn = dp[i];
  }
  cout << maxn << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int dp[1 << 24], x[8][8];
queue<int> Q;
int main() {
  int n;
  cin >> n;
  int minp = 0, maxp = 0;
  for (int i = 0; i < n; i++) {
    minp += (n - 1 - i) * (1 << (3 * i));
    maxp += i * (1 << (3 * i));
  }
  for (int i = minp; i <= maxp; i++)
    dp[i] = 1000000007;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  int G = 0, maxn = 0;
  for (int i = 0; i < n; i++) {
    G += (1 << (3 * i)) * i;
  }
  Q.push(G);
  dp[G] = 0;
  while (!Q.empty()) {
    int H = Q.front();
    Q.pop();
    int I[8], J[8];
    for (int i = 0; i < 8; i++)
      I[i] = (H / (1 << (3 * i))) % 8;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 8; k++)
          J[k] = I[k];
        swap(J[i], J[j]);
        int K = 0;
        for (int k = 0; k < 8; k++)
          K += (1 << (3 * k)) * J[k];
        if (dp[K] > dp[H] + x[i][j]) {
          dp[K] = dp[H] + x[i][j];
          Q.push(K);
        }
      }
    }
  }
  for (int i = minp; i <= maxp; i++) {
    if (dp[i] != 1000000007 && maxn < dp[i])
      maxn = dp[i];
  }
  cout << maxn << endl;
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define pb push_back
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(all(c)), c.end())
using namespace std;
typedef pair<int, int> P;
int main() {
  int n;
  vector<vector<int>> c(8, vector<int>(8));
  while (cin >> n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> c[i][j];
      }
    }
    priority_queue<P, vector<P>, greater<P>> que;
    int v = 0;
    for (int i = 0; i < n; i++) {
      v *= 10;
      v += i;
    }
    int ans = 0;
    map<int, bool> used;
    que.push(P(0, v));
    while (!que.empty()) {

      P p = que.top();
      que.pop();
      int now = p.second;
      if (used[now]) {
        continue;
      }
      int np = p.first;
      used[now] = true;
      ans = max(ans, np);
      int m = 1;
      for (int i = 0; i < n; i++) {
        m *= 10;
      }
      for (int i = 1; i < m; i *= 10)
        for (int j = i * 10; j < m; j *= 10) {
          int a = (now / i) % 10, b = (now / j) % 10;
          int nn = now - a * i - b * j + a * j + b * i;
          // swap(now[i],now[j]);
          if (used[nn]) {
            // swap(now[i],now[j]);
            continue;
          }
          // cout<<"--------"<<endl;
          que.push(P(np + c[a][b], nn));
          // swap(now[i],now[j]);
        }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define pb push_back
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(all(c)), c.end())
using namespace std;
typedef pair<int, vector<int>> P;
struct cmp {
  bool operator()(P a, P b) { return a.first > b.first; }
};

/*bool cmp(P a,P b){
        return a.first>b.first;
}*/
int main() {
  int n;
  vector<vector<int>> c(8, vector<int>(8));
  while (cin >> n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> c[i][j];
      }
    }
    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      v[i] = i;
    }
    int ans = 0;
    map<vector<int>, bool> used;
    que.push(P(0, v));
    while (!que.empty()) {

      P p = que.top();
      que.pop();
      vector<int> now = p.second;
      if (used[now]) {
        continue;
      }
      int np = p.first;
      used[now] = true;
      ans = max(ans, np);

      for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {

          swap(now[i], now[j]);
          if (used[now]) {
            swap(now[i], now[j]);
            continue;
          }
          // cout<<"--------"<<endl;
          que.push(P(np + c[now[i]][now[j]], now));
          swap(now[i], now[j]);
        }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01478 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int cost[9][9];

struct node {
  string num;
  int cost;
  node(string n, int c) : num(n), cost(c) {}
  node() {}
  bool operator>(const node &arg) const { return cost > arg.cost; }
};

int main() {
  int n;
  string str = "";
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cost[i][j];
    }
  }

  for (int i = 0; i < n; i++)
    str += (char)('1' + i);

  priority_queue<node, vector<node>, greater<node>> q;
  map<string, bool> mb;
  map<string, int> mi;
  string tmpstr, working;
  int tmpcost;
  node tmpnode;
  q.push(node(str, 0));
  while (!q.empty()) {
    tmpnode = q.top();
    q.pop();
    tmpstr = tmpnode.num;
    tmpcost = tmpnode.cost;

    if (mb[tmpstr] == true)
      continue;
    mb[tmpstr] = true;
    mi[tmpstr] = tmpcost;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        working = tmpstr;
        swap(working[i], working[j]);
        if (mb[working] == false) {
          q.push(node(working,
                      tmpcost + cost[working[i] - '1'][working[j] - '1']));
        }
      }
    }
  }
  map<string, int>::iterator it = mi.begin();
  int ret = -1;
  while (it != mi.end()) {
    ret = max(ret, (*it).second);
    ++it;
  }
  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int cost[9][9];

struct node {
  string num;
  int cost;
  node(string n, int c) : num(n), cost(c) {}
  node() {}
  bool operator>(const node &arg) const { return cost > arg.cost; }
};

int main() {
  int n;
  string str = "";
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cost[i][j];
    }
  }

  for (int i = 0; i < n; i++)
    str += (char)('1' + i);

  priority_queue<node, vector<node>, greater<node>> q;
  map<string, bool> mb;
  map<string, int> mi;
  string tmpstr, working;
  int tmpcost;
  node tmpnode;
  q.push(node(str, 0));
  while (!q.empty()) {
    tmpnode = q.top();
    q.pop();
    tmpstr = tmpnode.num;
    tmpcost = tmpnode.cost;

    if (mb[tmpstr] == true)
      continue;
    mb[tmpstr] = true;
    mi[tmpstr] = tmpcost;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        working = tmpstr;
        swap(working[i], working[j]);
        q.push(
            node(working, tmpcost + cost[working[i] - '1'][working[j] - '1']));
      }
    }
  }
  map<string, int>::iterator it = mi.begin();
  int ret = -1;
  while (it != mi.end()) {
    ret = max(ret, (*it).second);
    ++it;
  }
  cout << ret << endl;
}