prithivMLmods/Coder-Stat huggingface dataset:
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 10e15;

ll dist[205][205];
int N, M, R;

vector<vector<ll>> distforbitDP(10, vector<ll>(10, 0));

ll dp[(1 << 10) + 1][10];
ll rec(int bit, int v) {
  if (dp[bit][v] != -1)
    return dp[bit][v];

  if (bit == (1 << v)) {
    return dp[bit][v] = 0;
  }

  ll res = INF;

  int prev_bit = bit & ~(1 << v);

  for (int u = 0; u < N; ++u) {
    if (!(prev_bit & (1 << u)))
      continue;

    if (res > rec(prev_bit, u) + distforbitDP[u][v]) {
      res = rec(prev_bit, u) + distforbitDP[u][v];
    }
  }

  return dp[bit][v] = res;
}

int main() {
  cin >> N >> M >> R;

  vector<int> r(R);
  rep(i, R) cin >> r[i], --r[i];

  rep(i, N) rep(j, N) dist[i][j] = INF;

  rep(i, M) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    dist[a][b] = t;
    dist[b][a] = t;
  }

  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    }
  }
  rep(i, R) rep(j, R) distforbitDP[i][j] = dist[r[i]][r[j]];

  for (int bit = 0; bit < (1 << R); ++bit) {
    for (int v = 0; v < R; ++v) {
      dp[bit][v] = -1;
    }
  }

  ll res = INF;
  for (int v = 0; v < R; ++v) {
    if (res > rec((1 << R) - 1, v)) {
      res = rec((1 << R) - 1, v);
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 10e15;

ll dist[205][205];
int N, M, R;

vector<vector<ll>> distforbitDP(10, vector<ll>(10, 0));

ll dp[(1 << 10) + 1][10];
ll rec(int bit, int v) {
  if (dp[bit][v] != -1)
    return dp[bit][v];

  if (bit == (1 << v)) {
    return dp[bit][v] = 0;
  }

  ll res = INF;

  int prev_bit = bit & ~(1 << v);

  for (int u = 0; u < R; ++u) {
    if (!(prev_bit & (1 << u)))
      continue;

    if (res > rec(prev_bit, u) + distforbitDP[u][v]) {
      res = rec(prev_bit, u) + distforbitDP[u][v];
    }
  }

  return dp[bit][v] = res;
}

int main() {
  cin >> N >> M >> R;

  vector<int> r(R);
  rep(i, R) cin >> r[i], --r[i];

  rep(i, N) rep(j, N) dist[i][j] = INF;

  rep(i, M) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    dist[a][b] = t;
    dist[b][a] = t;
  }

  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    }
  }
  rep(i, R) rep(j, R) distforbitDP[i][j] = dist[r[i]][r[j]];

  for (int bit = 0; bit < (1 << R); ++bit) {
    for (int v = 0; v < R; ++v) {
      dp[bit][v] = -1;
    }
  }

  ll res = INF;
  for (int v = 0; v < R; ++v) {
    if (res > rec((1 << R) - 1, v)) {
      res = rec((1 << R) - 1, v);
    }
  }
  cout << res << endl;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

// long longのシフト演算には気をつけよう

const ll INF = (1LL << 60);

int N, R;
ll res;
ll d[205][205];
int r[10];
bool used[10];

void dfs(int c, int las, int dist) {
  if (c == R) {
    if (res > dist)
      res = dist;
    return;
  }
  for (int i = 1; i <= R; i++)
    if (!used[r[i]]) {
      used[r[i]] = true;
      if (las == -1)
        dfs(c + 1, i, 0);
      else
        dfs(c + 1, i, dist + d[r[las]][r[i]]);
      used[r[i]] = false;
    }
}

void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      if (d[i][k] == INF)
        continue;
      for (int j = 1; j <= N; j++) {
        if (d[k][j] == INF)
          continue;
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {
  int M;
  cin >> N >> M >> R;
  for (int i = 1; i <= R; i++)
    cin >> r[i];

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      d[i][j] = ((i == j) ? 0 : INF);
  }

  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    d[a][b] = d[b][a] = c;
  }

  floyd();

  res = INF;
  dfs(0, -1, 0);
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

// long longのシフト演算には気をつけよう

const ll INF = (1LL << 60);

int N, R;
ll res;
ll d[205][205];
int r[10];
bool used[205];

void dfs(int c, int las, int dist) {
  if (c == R) {
    if (res > dist)
      res = dist;
    return;
  }
  for (int i = 1; i <= R; i++)
    if (!used[r[i]]) {
      used[r[i]] = true;
      if (las == -1)
        dfs(c + 1, i, 0);
      else
        dfs(c + 1, i, dist + d[r[las]][r[i]]);
      used[r[i]] = false;
    }
}

void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      if (d[i][k] == INF)
        continue;
      for (int j = 1; j <= N; j++) {
        if (d[k][j] == INF)
          continue;
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {
  int M;
  cin >> N >> M >> R;
  for (int i = 1; i <= R; i++)
    cin >> r[i];

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      d[i][j] = ((i == j) ? 0 : INF);
  }

  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    d[a][b] = d[b][a] = c;
  }

  floyd();

  res = INF;
  dfs(0, -1, 0);
  cout << res << endl;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
using namespace std;
using ll = long long;
const int MAX_N = 110;
const ll MAX_D = 1000000000;
int dis[MAX_N][MAX_N];

int best_d = MAX_D;
vector<int> r_city;

void dfs(int now, vector<int> visited, int d) {
  bool update = false;
  rep(i, 0, visited.size()) if (visited[i] == 0) {
    update = true;
    int next = r_city[i];
    visited[i] = 1;
    dfs(next, visited, d + dis[now][next]);
    visited[i] = 0;
  }

  if (!update)
    best_d = min(best_d, d);
}

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  rep(i, 0, r) {
    int rr;
    cin >> rr;
    rr--;
    r_city.push_back(rr);
  };

  rep(i, 0, n) rep(j, 0, n) {
    if (i == j)
      continue;
    dis[i][j] = MAX_D;
  }
  rep(i, 0, n) dis[n][i] = 0;

  rep(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    dis[a][b] = c;
    dis[b][a] = c;
  }

  rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
    if (dis[i][k] == MAX_D or dis[k][j] == MAX_D)
      continue;
    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  }

  vector<int> visited(r_city.size(), 0);
  dfs(n, visited, 0);

  cout << best_d << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
using namespace std;
using ll = long long;
const int MAX_N = 310;
const ll MAX_D = 1000000000;
int dis[MAX_N][MAX_N];

int best_d = MAX_D;
vector<int> r_city;

void dfs(int now, vector<int> visited, int d) {
  bool update = false;
  rep(i, 0, visited.size()) if (visited[i] == 0) {
    update = true;
    int next = r_city[i];
    visited[i] = 1;
    dfs(next, visited, d + dis[now][next]);
    visited[i] = 0;
  }

  if (!update)
    best_d = min(best_d, d);
}

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  rep(i, 0, r) {
    int rr;
    cin >> rr;
    rr--;
    r_city.push_back(rr);
  };

  rep(i, 0, n) rep(j, 0, n) {
    if (i == j)
      continue;
    dis[i][j] = MAX_D;
  }
  rep(i, 0, n) dis[n][i] = 0;

  rep(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    dis[a][b] = c;
    dis[b][a] = c;
  }

  rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
    if (dis[i][k] == MAX_D or dis[k][j] == MAX_D)
      continue;
    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  }

  vector<int> visited(r_city.size(), 0);
  dfs(n, visited, 0);

  cout << best_d << endl;
  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m, R;
  cin >> n >> m >> R;

  int r[R + 1];
  for (int i = 1; i <= R; ++i)
    cin >> r[i];

  const int INF = (int)1e9;

  int cost[n + 1][n + 1];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cost[i][j] = INF;

  for (int i = 1; i <= m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    cost[a][b] = cost[b][c] = min(cost[a][b], c);
  }

  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);

  sort(r + 1, r + R + 1);

  int minCost = INF;

  do {
    int totalCost = 0;
    for (int i = 2; i <= R; ++i)
      totalCost += cost[r[i]][r[i - 1]];

    minCost = min(minCost, totalCost);
  } while (next_permutation(r + 1, r + R + 1));

  cout << minCost;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m, R;
  cin >> n >> m >> R;

  int r[R + 1];
  for (int i = 1; i <= R; ++i)
    cin >> r[i];

  const int INF = (int)1e9;

  int cost[n + 1][n + 1];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cost[i][j] = INF;

  for (int i = 1; i <= m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    cost[a][b] = cost[b][a] = min(cost[a][b], c);
  }

  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);

  sort(r + 1, r + R + 1);

  int minCost = INF;

  do {
    int totalCost = 0;
    for (int i = 2; i <= R; ++i)
      totalCost += cost[r[i]][r[i - 1]];

    minCost = min(minCost, totalCost);
  } while (next_permutation(r + 1, r + R + 1));

  cout << minCost;

  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> p(r);
  for (int i = 0; i < r; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> g(m, vector<int>(m, INF));
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    g[a][b] = c;
    g[b][a] = c;
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  sort(p.begin(), p.end());
  int ans = INF;
  do {
    int dist = 0;
    for (int i = 0; i < r - 1; ++i) {
      dist += g[p[i]][p[i + 1]];
    }
    ans = min(ans, dist);
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> p(r);
  for (int i = 0; i < r; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> g(n, vector<int>(n, INF));
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    g[a][b] = c;
    g[b][a] = c;
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  sort(p.begin(), p.end());
  int ans = INF;
  do {
    int dist = 0;
    for (int i = 0; i < r - 1; ++i) {
      dist += g[p[i]][p[i + 1]];
    }
    ans = min(ans, dist);
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

const int INF = 1e9;
vecveci dist(110, veci(110, INF));

int main() {
  int N, M, R;
  cin >> N >> M >> R;

  vector<int> ri(R);
  REP(i, R) {
    cin >> ri[i];
    ri[i]--;
  }

  REP(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    dist[a][b] = min(dist[a][b], c);
    dist[b][a] = min(dist[b][a], c);
  }

  REP(k, N)
  REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  int ans = INF;

  sort(ri.begin(), ri.end());

  do {
    int total = 0;
    REP(i, R - 1) { total += dist[ri[i]][ri[i + 1]]; }
    chmin(ans, total);
  } while (next_permutation(ri.begin(), ri.end()));

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

const int INF = 1e9;
vecveci dist(210, veci(210, INF));

int main() {
  int N, M, R;
  cin >> N >> M >> R;

  vector<int> ri(R);
  REP(i, R) {
    cin >> ri[i];
    ri[i]--;
  }

  REP(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    dist[a][b] = min(dist[a][b], c);
    dist[b][a] = min(dist[b][a], c);
  }

  REP(k, N)
  REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  int ans = INF;

  sort(ri.begin(), ri.end());

  do {
    int total = 0;
    REP(i, R - 1) { total += dist[ri[i]][ri[i + 1]]; }
    chmin(ans, total);
  } while (next_permutation(ri.begin(), ri.end()));

  cout << ans << endl;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n, m, R;
  cin >> n >> m >> R;

  vector<int> X(R);
  for (int i = 0; i < R; ++i) {
    cin >> X[i];
    --X[i];
  }

  const long long INF = 1LL << 50;
  vector<vector<long long>> dis(n, vector<long long>(m, INF));

  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;

    dis[u][v] = min(dis[u][v], (long long)w);
    dis[v][u] = min(dis[v][u], (long long)w);
  }
  for (int i = 0; i < n; ++i) {
    dis[i][i] = 0;
  }

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }

  vector<int> ord(R, 0);
  iota(ord.begin(), ord.end(), 0);

  long long res = INF;
  do {
    long long d = 0;
    for (int i = 1; i < R; ++i) {
      int u = X[ord[i - 1]], v = X[ord[i]];
      d += dis[u][v];
    }
    res = min(res, d);
  } while (next_permutation(ord.begin(), ord.end()));

  cout << res << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n, m, R;
  cin >> n >> m >> R;

  vector<int> X(R);
  for (int i = 0; i < R; ++i) {
    cin >> X[i];
    --X[i];
  }

  const long long INF = 1LL << 50;
  vector<vector<long long>> dis(n, vector<long long>(n, INF));

  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;

    dis[u][v] = min(dis[u][v], (long long)w);
    dis[v][u] = min(dis[v][u], (long long)w);
  }
  for (int i = 0; i < n; ++i) {
    dis[i][i] = 0;
  }

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }

  vector<int> ord(R, 0);
  iota(ord.begin(), ord.end(), 0);

  long long res = INF;
  do {
    long long d = 0;
    for (int i = 1; i < R; ++i) {
      int u = X[ord[i - 1]], v = X[ord[i]];
      d += dis[u][v];
    }
    res = min(res, d);
  } while (next_permutation(ord.begin(), ord.end()));

  cout << res << '\n';

  return 0;
}
Problem p03608 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import permutations

N, M, R = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
G = [[0] * N for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    G[a][b] = c
    G[b][a] = c

# ワーシャルフロイド
for k in range(N):
    for i in range(N):
        for j in range(N):
            G[i][j] = min(G[i][j], G[i][k] + G[k][j])

# 街の訪れ方を全探索
ans = float("inf")
for perm in permutations(r):
    tmp = 0
    for i in range(R - 1):
        tmp += G[perm[i]][perm[i + 1]]
    ans = min(ans, tmp)

print(ans)


Fixed submission:
from itertools import permutations

N, M, R = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
G = [[float("inf")] * N for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    G[a][b] = c
    G[b][a] = c

# ワーシャルフロイド
for k in range(N):
    for i in range(N):
        for j in range(N):
            G[i][j] = min(G[i][j], G[i][k] + G[k][j])

# 街の訪れ方を全探索
ans = float("inf")
for perm in permutations(r):
    tmp = 0
    for i in range(R - 1):
        tmp += G[perm[i]][perm[i + 1]]
    ans = min(ans, tmp)

print(ans)
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, L, R, H, W;

long long int ans = LLONG_MAX;
vector<vector<long long int>> edge;
int place[10] = {};

void Search(vector<int> v, int num, long long int sum, int last) {
  // cout << num << " " << last << " " << sum << endl;
  for (int i = 0; i < K; i++) {
    if (v[i]) {
      continue;
    }
    v[i] = 1;
    Search(v, num + 1, sum + edge[last][place[i]], place[i]);
    v[i] = 0;
  }
  if (num == K) {
    ans = min(ans, sum);
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M >> K;
  for (int i = 0; i < K; i++) {
    cin >> place[i];
  }
  edge.resize(N + 1, vector<long long int>(N + 1, INT_MAX));
  for (int i = 0; i < M; i++) {
    cin >> L >> R >> H;
    edge[L][R] = H;
    edge[R][L] = H;
  }
  for (int i = 1; i <= N; i++) {
    edge[i][i] = 0;
  }
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]);
      }
    }
  }
  vector<int> box(K, 0);
  for (int i = 0; i < N; i++) {
    box[i] = 1;
    Search(box, 1, 0, place[i]);
    box[i] = 0;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, L, R, H, W;

long long int ans = LLONG_MAX;
vector<vector<long long int>> edge;
int place[10] = {};

void Search(vector<int> v, int num, long long int sum, int last) {
  // cout << num << " " << last << " " << sum << endl;
  for (int i = 0; i < K; i++) {
    if (v[i]) {
      continue;
    }
    v[i] = 1;
    Search(v, num + 1, sum + edge[last][place[i]], place[i]);
    v[i] = 0;
  }
  if (num == K) {
    ans = min(ans, sum);
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M >> K;
  for (int i = 0; i < K; i++) {
    cin >> place[i];
  }
  edge.resize(N + 1, vector<long long int>(N + 1, INT_MAX));
  for (int i = 0; i < M; i++) {
    cin >> L >> R >> H;
    edge[L][R] = H;
    edge[R][L] = H;
  }
  for (int i = 1; i <= N; i++) {
    edge[i][i] = 0;
  }
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]);
      }
    }
  }
  vector<int> box(K, 0);
  for (int i = 0; i < K; i++) {
    box[i] = 1;
    Search(box, 1, 0, place[i]);
    box[i] = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
#define INF (1 << 29)
int N, M, R;
int d[201][201];
int r[9];
int A, B, C;
int res;
bool used[9];

void dfs(int c, int las, int dist) {
  if (c == R) {
    if (res > dist)
      res = dist;
    return;
  }
  for (int i = 1; i <= R; i++)
    if (!used[r[i]]) {
      used[r[i]] = true;
      if (las == -1)
        dfs(c + 1, r[i], 0);
      else
        dfs(c + 1, r[i], dist + d[las][r[i]]);
      used[r[i]] = false;
    }
}

int main() {
  scanf("%d%d%d", &N, &M, &R);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      if (i != j)
        d[i][j] = INF;
  for (int i = 1; i <= R; i++)
    scanf("%d", &r[i]);
  for (int i = 1; i <= M; i++) {
    scanf("%d%d%d", &A, &B, &C);
    if (d[A][B] > C)
      d[A][B] = d[B][A] = C;
  }
  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        if (d[i][j] > d[i][k] + d[k][j])
          d[i][j] = d[i][k] + d[k][j];
  res = INF;
  dfs(0, -1, 0);
  printf("%d\n", res);
}

Fixed submission:
#include <cstdio>
using namespace std;
#define INF (1 << 29)
int N, M, R;
int d[201][201];
int r[9];
int A, B, C;
int res;
bool used[201];

void dfs(int c, int las, int dist) {
  if (c == R) {
    if (res > dist)
      res = dist;
    return;
  }
  for (int i = 1; i <= R; i++)
    if (!used[r[i]]) {
      used[r[i]] = true;
      if (las == -1)
        dfs(c + 1, r[i], 0);
      else
        dfs(c + 1, r[i], dist + d[las][r[i]]);
      used[r[i]] = false;
    }
}

int main() {
  scanf("%d%d%d", &N, &M, &R);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      if (i != j)
        d[i][j] = INF;
  for (int i = 1; i <= R; i++)
    scanf("%d", &r[i]);
  for (int i = 1; i <= M; i++) {
    scanf("%d%d%d", &A, &B, &C);
    if (d[A][B] > C)
      d[A][B] = d[B][A] = C;
  }
  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        if (d[i][j] > d[i][k] + d[k][j])
          d[i][j] = d[i][k] + d[k][j];
  res = INF;
  dfs(0, -1, 0);
  printf("%d\n", res);
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 7;
vector<vector<int>> dist(200, vector<int>(200, inf));

int main() {
  int n, m, r;
  cin >> n >> m >> r;

  vector<int> towns(r);

  for (int i = 0; i < r; i++)
    cin >> towns[i];

  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    dist[x][y] = c;
    dist[y][x] = c;
  }

  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
      }
    }
  }

  int ans = 1e9;
  sort(towns.begin(), towns.end());

  do {
    int d = 0;
    for (int i = 1; i < (int)towns.size(); i++) {
      d += dist[towns[i - 1]][towns[i]];
    }
    ans = min(ans, d);
  } while (next_permutation(towns.begin(), towns.end()));

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 7;
vector<vector<int>> dist(201, vector<int>(201, inf));

int main() {
  int n, m, r;
  cin >> n >> m >> r;

  vector<int> towns(r);

  for (int i = 0; i < r; i++)
    cin >> towns[i];

  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    dist[x][y] = c;
    dist[y][x] = c;
  }

  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
      }
    }
  }

  int ans = 1e9;
  sort(towns.begin(), towns.end());

  do {
    int d = 0;
    for (int i = 1; i < (int)towns.size(); i++) {
      d += dist[towns[i - 1]][towns[i]];
    }
    ans = min(ans, d);
  } while (next_permutation(towns.begin(), towns.end()));

  cout << ans;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
const int IINF = 1000000000;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M, R;
  cin >> N >> M >> R;
  vector<int> r(R);
  rep(i, R) cin >> r[i];
  rep(i, R) r[i]--;

  vector<vector<ll>> cost(N, vector<ll>(N, INF));

  // rep(i,N) cost[i][i] = 0;
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    // 無向グラフ
    cost[a][b] = c;
    cost[b][a] = c;
  }

  rep(k, N + 1) {
    rep(i, N + 1) {
      rep(j, N + 1) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); }
    }
  }
  ll ans = INF;
  // next_permutationを使う場合は、昇順でソートしておく。
  sort(r.begin(), r.end());
  // 経路を全パターン試す。
  do {
    ll tmp = 0;
    rep(i, R - 1) { tmp += cost[r[i]][r[i + 1]]; }
    ans = min(ans, tmp);

  } while (next_permutation(r.begin(), r.end()));

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
const int IINF = 1000000000;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M, R;
  cin >> N >> M >> R;
  vector<int> r(R);
  rep(i, R) cin >> r[i];
  rep(i, R) r[i]--;

  vector<vector<ll>> cost(N, vector<ll>(N, INF));

  // rep(i,N) cost[i][i] = 0;
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    // 無向グラフ
    cost[a][b] = c;
    cost[b][a] = c;
  }

  rep(k, N) {
    rep(i, N) {
      rep(j, N) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); }
    }
  }
  ll ans = INF;
  // next_permutationを使う場合は、昇順でソートしておく。
  sort(r.begin(), r.end());
  // 経路を全パターン試す。
  do {
    ll tmp = 0;
    rep(i, R - 1) { tmp += cost[r[i]][r[i + 1]]; }
    ans = min(ans, tmp);

  } while (next_permutation(r.begin(), r.end()));

  cout << ans << endl;
}
Problem p03608 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from itertools import permutations


def input():
    return sys.stdin.readline().strip()


def main():
    N, M, R = map(int, input().split())

    def warshall_floyd(d):
        # d[i][j]: iからjへの最短距離
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d

    r = list(map(int, input().split()))
    d = [[float("inf")] * N for i in range(N)]
    for i in range(M):
        x, y, z = map(int, input().split())
        d[x - 1][y - 1] = z
        d[y - 1][x - 1] = z
    for i in range(N):
        d[i][i] = 0  # 自身のところに行くコストは０
    D = warshall_floyd(d)
    P = list(permutations(r))
    cost = float("inf")
    for p in P:
        c = 0
        for i in range(R - 1):
            c += D[p[i] - 1][p[i + 1] - 1]
        if c < cost:
            cost = c
    print(cost)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from itertools import permutations


def input():
    return sys.stdin.readline().strip()


def main():
    N, M, R = map(int, input().split())

    def warshall_floyd(d):
        # d[i][j]: iからjへの最短距離
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    tmp = d[i][k] + d[k][j]
                    if d[i][j] > tmp:
                        d[i][j] = tmp
        return d

    r = list(map(int, input().split()))
    d = [[float("inf")] * N for i in range(N)]
    for i in range(M):
        x, y, z = map(int, input().split())
        d[x - 1][y - 1] = z
        d[y - 1][x - 1] = z
    for i in range(N):
        d[i][i] = 0  # 自身のところに行くコストは０
    D = warshall_floyd(d)
    P = list(permutations(r))
    cost = float("inf")
    for p in P:
        c = 0
        for i in range(R - 1):
            c += D[p[i] - 1][p[i + 1] - 1]
        if c < cost:
            cost = c
    print(cost)


if __name__ == "__main__":
    main()
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

const ll INF = 1e15;

int n;
ll d[201][201];

void warshallfloyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[j][k]);
      }
    }
  }
}

int main() {
  int m, r;
  cin >> n >> m >> r;
  int l[10];
  for (int i = 0; i < r; i++) {
    cin >> l[i];
    l[i]--;
  }
  for (int i = 0; i < n; i++) {
    fill(d[i], d[i] + n, INF);
  }
  int a[201], b[201];
  ll c[201];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    d[a[i] - 1][b[i] - 1] = c[i];
    d[b[i] - 1][a[i] - 1] = c[i];
  }
  warshallfloyd();
  ll ans = INF;
  int enu[10];
  for (int i = 0; i < r; i++)
    enu[i] = i;
  do {
    ll dist = 0;
    for (int i = 0; i < r - 1; i++) {
      dist += d[l[enu[i]]][l[enu[i + 1]]];
    }
    ans = min(ans, dist);
  } while (next_permutation(enu, enu + r));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

const ll INF = 1e15;

int n;
ll d[201][201];

void warshallfloyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[j][k]);
      }
    }
  }
}

int main() {
  int m, r;
  cin >> n >> m >> r;
  int l[10];
  for (int i = 0; i < r; i++) {
    cin >> l[i];
    l[i]--;
  }
  for (int i = 0; i < n; i++) {
    fill(d[i], d[i] + n, INF);
  }
  int a[40000], b[40000];
  ll c[40000];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    d[a[i] - 1][b[i] - 1] = c[i];
    d[b[i] - 1][a[i] - 1] = c[i];
  }
  warshallfloyd();
  ll ans = INF;
  int enu[10];
  for (int i = 0; i < r; i++)
    enu[i] = i;
  do {
    ll dist = 0;
    for (int i = 0; i < r - 1; i++) {
      dist += d[l[enu[i]]][l[enu[i + 1]]];
    }
    ans = min(ans, dist);
  } while (next_permutation(enu, enu + r));
  cout << ans << endl;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;

template <typename T> void warshall_floyd(vector<vector<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++) {
        if (g[i][k] == INF || g[k][j] == INF)
          continue;
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
}

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<int>> to(n, vector<int>(n, INF));
  rep(i, n) to[i][i] = 0;
  int d[r];
  rep(i, r) {
    cin >> d[i];
    d[i]--;
  }
  sort(d, d + r);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    to[a][b] = c;
    to[b][a] = c;
  }
  warshall_floyd(to);
  ll ans = LINF;
  do {
    ll res = 0;
    rep(i, n - 1) { res += to[d[i]][d[i + 1]]; }
    chmin(ans, res);
  } while (next_permutation(d, d + r));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;

template <typename T> void warshall_floyd(vector<vector<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++) {
        if (g[i][k] == INF || g[k][j] == INF)
          continue;
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
}

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<int>> to(n, vector<int>(n, INF));
  rep(i, n) to[i][i] = 0;
  int d[r];
  rep(i, r) {
    cin >> d[i];
    d[i]--;
  }
  sort(d, d + r);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    to[a][b] = c;
    to[b][a] = c;
  }
  warshall_floyd(to);
  ll ans = LINF;
  do {
    ll res = 0;
    rep(i, r - 1) { res += to[d[i]][d[i + 1]]; }
    chmin(ans, res);
  } while (next_permutation(d, d + r));
  cout << ans << endl;
  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long LL;

const int INF = int(1e9);
int N, M, R;
int Map[210][210];
int a[10];
int visit[10];
int min1;

void dfs(int n, int i, int sum) {
  if (n >= R + 1) {
    if (min1 > sum)
      min1 = sum;
    return;
  }
  for (int j = 0; j < R; j++)
    if (!visit[j]) {
      visit[j] = 1;
      if (i == -1)
        dfs(n + 1, j, 0);
      else
        dfs(n + 1, a[j], sum + Map[a[i]][a[j]]);
      visit[j] = 0;
    }
}

int main() {
  cin >> N >> M >> R;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++)
      if (i != j)
        Map[i][j] = INF;

  for (int i = 0; i < R; i++)
    cin >> a[i];
  for (int i = 0; i < M; i++) {
    int x, y, len;
    cin >> x >> y >> len;
    if (Map[x][y] > len)
      Map[x][y] = Map[y][x] = len;
  }

  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        Map[i][j] = min(Map[i][j], Map[i][k] + Map[k][j]);

  min1 = INF;
  dfs(1, -1, 0);
  cout << min1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long LL;

const int INF = int(1e9);
int N, M, R;
int Map[210][210];
int a[10];
int visit[10];
int min1;

void dfs(int n, int i, int sum) {
  if (n >= R + 1) {
    if (min1 > sum)
      min1 = sum;
    return;
  }
  for (int j = 0; j < R; j++)
    if (!visit[j]) {
      visit[j] = 1;
      if (i == -1)
        dfs(n + 1, j, 0);
      else
        dfs(n + 1, j, sum + Map[a[i]][a[j]]);
      visit[j] = 0;
    }
}

int main() {
  cin >> N >> M >> R;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++)
      if (i != j)
        Map[i][j] = INF;

  for (int i = 0; i < R; i++)
    cin >> a[i];
  for (int i = 0; i < M; i++) {
    int x, y, len;
    cin >> x >> y >> len;
    if (Map[x][y] > len)
      Map[x][y] = Map[y][x] = len;
  }

  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        Map[i][j] = min(Map[i][j], Map[i][k] + Map[k][j]);

  min1 = INF;
  dfs(1, -1, 0);
  cout << min1 << endl;

  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

static const int MAX = 100;
static const int INF = 1e9;

// 階乗
int factorial(int n) {
  if (n == 0)
    return 1;
  return n * factorial(n - 1);
}

// ワ―シャルフロイド法 O(V^3)
int n, M[MAX][MAX];
// main関数からこれらを削除すること

void warshall_floyd() {           // 隣接行列Mを書き換える
  for (int i = 0; i < n; i++)     // 経由する頂点
    for (int j = 0; j < n; j++)   // 始点
      for (int k = 0; k < n; k++) // 終点
        M[j][k] = min(M[j][k], M[j][i] + M[i][k]);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // 重み付き無向(有向)グラフの隣接行列
  int m, r, u, v, w;
  cin >> n >> m >> r;

  vector<int> p(r);
  for (int i = 0; i < r; i++)
    cin >> p.at(i);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = INF; // 大きな数で初期化
    }
  }

  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    u--;
    v--;
    M[u][v] = w;
    M[v][u] = w; // 重み付き有向グラフではこの行をコメントアウト
  }

  warshall_floyd();

  long long ans = 1e15;
  for (int i = 0; i < factorial(r); i++) {
    long long keep = 0;
    for (int j = 0; j < r - 1; j++) {
      keep += M[p.at(j) - 1][p.at(j + 1) - 1];
    }
    ans = min(ans, keep);
    next_permutation(p.begin(), p.end());
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

static const int MAX = 200;
static const int INF = 1e9;

// 階乗
int factorial(int n) {
  if (n == 0)
    return 1;
  return n * factorial(n - 1);
}

// ワ―シャルフロイド法 O(V^3)
int n, M[MAX][MAX];
// main関数からこれらを削除すること

void warshall_floyd() {           // 隣接行列Mを書き換える
  for (int i = 0; i < n; i++)     // 経由する頂点
    for (int j = 0; j < n; j++)   // 始点
      for (int k = 0; k < n; k++) // 終点
        M[j][k] = min(M[j][k], M[j][i] + M[i][k]);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // 重み付き無向(有向)グラフの隣接行列
  int m, r, u, v, w;
  cin >> n >> m >> r;

  vector<int> p(r);
  for (int i = 0; i < r; i++)
    cin >> p.at(i);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = INF; // 大きな数で初期化
    }
  }

  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    u--;
    v--;
    M[u][v] = w;
    M[v][u] = w; // 重み付き有向グラフではこの行をコメントアウト
  }

  warshall_floyd();

  long long ans = 1e15;
  for (int i = 0; i < factorial(r); i++) {
    long long keep = 0;
    for (int j = 0; j < r - 1; j++) {
      keep += M[p.at(j) - 1][p.at(j + 1) - 1];
    }
    ans = min(ans, keep);
    next_permutation(p.begin(), p.end());
  }

  cout << ans << "\n";
}
Problem p03608 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, ll> pa;

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int d[300][300];
int a[10];
int n, m, r;
void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      if (d[i][k] != inf)
        for (int j = 1; j <= n; j++) {
          d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
        }
    }
  }
}
ll slove() {
  ll ans = 0;
  for (int i = 1; i < r; i++) {
    ans += d[a[i]][a[i + 1]];
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> r;
  for (int i = 1; i <= r; i++)
    cin >> a[i];
  memset(d, 0x3f, sizeof d);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    d[a][b] = d[b][a] = c;
  }
  floyd();
  ll ans = 1e18;
  sort(a + 1, a + 1 + r);
  do {
    ans = min(ans, slove());
  } while (next_permutation(a + 1, a + 1 + n));

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, ll> pa;

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int d[300][300];
int a[10];
int n, m, r;
void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      if (d[i][k] != inf)
        for (int j = 1; j <= n; j++) {
          d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
        }
    }
  }
}
ll slove() {
  ll ans = 0;
  for (int i = 1; i < r; i++) {
    ans += d[a[i]][a[i + 1]];
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> r;
  for (int i = 1; i <= r; i++)
    cin >> a[i];
  memset(d, 0x3f, sizeof d);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    d[a][b] = d[b][a] = c;
  }
  floyd();
  ll ans = 1e18;
  sort(a + 1, a + 1 + r);
  do {
    ans = min(ans, slove());
  } while (next_permutation(a + 1, a + 1 + r));

  cout << ans << endl;
  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int a, b, c, r[200];
int dep[200][200];
bool used[200];
int N, M, R;
long long ans = 0;
long long inf = 1000000007;

void dfs(int c, int las, int dist) {
  if (c == R + 1) {
    if (ans > dist)
      ans = dist;
    return;
  }
  for (int i = 1; i <= R; i++) {
    if (!used[i]) {
      used[i] = true;
      if (las == -1)
        dfs(c + 1, i, 0);
      else {
        dfs(c + 1, i, dist + dep[r[las]][r[i]]);
      }
      used[i] = false;
    }
  }
}
void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        dep[i][j] = min(dep[i][j], dep[i][k] + dep[k][j]);
      }
    }
  }
}
int main() {
  cin >> N >> M >> R;
  for (int i = 1; i <= R; i++) {
    cin >> r[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i != j)
        dep[i][j] = inf;
    }
  }
  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c;
    if (dep[a][b] > c) {
      dep[a][b] = dep[b][a] = c;
    }
  }
  floyd();
  ans = inf;
  dfs(1, -1, 0);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int a, b, c, r[201];
int dep[201][201];
bool used[201];
int N, M, R;
long long ans = 0;
long long inf = 1000000007;

void dfs(int c, int las, int dist) {
  if (c == R + 1) {
    if (ans > dist)
      ans = dist;
    return;
  }
  for (int i = 1; i <= R; i++) {
    if (!used[i]) {
      used[i] = true;
      if (las == -1)
        dfs(c + 1, i, 0);
      else {
        dfs(c + 1, i, dist + dep[r[las]][r[i]]);
      }
      used[i] = false;
    }
  }
}
void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        dep[i][j] = min(dep[i][j], dep[i][k] + dep[k][j]);
      }
    }
  }
}
int main() {
  cin >> N >> M >> R;
  for (int i = 1; i <= R; i++) {
    cin >> r[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i != j)
        dep[i][j] = inf;
    }
  }
  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c;
    if (dep[a][b] > c) {
      dep[a][b] = dep[b][a] = c;
    }
  }
  floyd();
  ans = inf;
  dfs(1, -1, 0);
  cout << ans << endl;
  return 0;
}
Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 200000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M, R;
  int d[200][200];
  cin >> N >> M >> R;
  int r[R];
  rep(i, R) {
    cin >> r[i];
    r[i]--;
  }
  rep(i, 200) {
    rep(j, 200) {
      if (i != j)
        d[i][j] = INF;
    }
  }
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    d[a][b] = c;
    d[b][a] = c;
  }
  rep(k, M) {
    rep(i, M) {
      rep(j, M) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
    }
  }
  sort(r, r + R);
  int d_m = INF;
  do {
    int tmp = 0;
    rep(i, R - 1) {
      // dump(tmp);
      tmp += d[r[i]][r[i + 1]];
    }
    d_m = min(d_m, tmp);
  } while (next_permutation(r, r + R));
  cout << d_m << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 200000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M, R;
  int d[200][200];
  cin >> N >> M >> R;
  int r[R];
  rep(i, R) {
    cin >> r[i];
    r[i]--;
  }
  rep(i, 200) {
    rep(j, 200) {
      if (i != j)
        d[i][j] = INF;
    }
  }
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    d[a][b] = c;
    d[b][a] = c;
  }
  rep(k, N) {
    rep(i, N) {
      rep(j, N) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
    }
  }
  sort(r, r + R);
  int d_m = INF;
  do {
    int tmp = 0;
    rep(i, R - 1) {
      // dump(tmp);
      tmp += d[r[i]][r[i + 1]];
    }
    d_m = min(d_m, tmp);
  } while (next_permutation(r, r + R));
  cout << d_m << endl;

  return 0;
}