Problem p03608 (C++)

Buggy submission (status: Memory Limit Exceeded):
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = int64_t;
using ull = uint64_t;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

int main() {
  ll N, M, R;
  cin >> N >> M >> R;
  vector<ll> ris;
  for (ll i = 0; i < R; i++) {
    ll ri;
    cin >> ri;
    ri--;
    ris.push_back(ri);
  }
  auto distances = make_vec_nd(N, N, 200000);
  for (ll m = 0; m < M; m++) {
    ll A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    distances[A][B] = C;
    distances[B][A] = C;
  }
  for (ll k = 0; k < N; k++) {
    for (ll i = 0; i < N; i++) {
      for (ll j = 0; j < N; j++) {
        distances[i][j] =
            min(distances[i][j], distances[i][k] + distances[k][j]);
      }
    }
  }
  vector<ll> perm;
  for (ll i = 0; i < R; i++) {
    perm.push_back(i);
  }
  ll ans = numeric_limits<ll>::max();
  do {
    ll dist = 0;
    for (ll i = 0; i < perm.size() - 1; i++) {
      dist += distances[ris[perm[i]]][ris[perm[i + 1]]];
    }
    ans = min(ans, dist);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
}


Fixed submission:
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = int64_t;
using ull = uint64_t;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

int main() {
  ll N, M, R;
  cin >> N >> M >> R;
  vector<ll> ris;
  for (ll i = 0; i < R; i++) {
    ll ri;
    cin >> ri;
    ri--;
    ris.push_back(ri);
  }
  auto distances = make_vec_nd(numeric_limits<ll>::max() / 2, N, N);
  for (ll m = 0; m < M; m++) {
    ll A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    distances[A][B] = C;
    distances[B][A] = C;
  }
  for (ll k = 0; k < N; k++) {
    for (ll i = 0; i < N; i++) {
      for (ll j = 0; j < N; j++) {
        distances[i][j] =
            min(distances[i][j], distances[i][k] + distances[k][j]);
      }
    }
  }
  vector<ll> perm;
  for (ll i = 0; i < R; i++) {
    perm.push_back(i);
  }
  ll ans = numeric_limits<ll>::max();
  do {
    ll dist = 0;
    for (ll i = 0; i < perm.size() - 1; i++) {
      dist += distances[ris[perm[i]]][ris[perm[i + 1]]];
    }
    ans = min(ans, dist);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
}