Problem p03608 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
using gra = vector<vector<ll>>;
using gram = vector<unordered_map<ll, ll>>;

typedef unsigned long long ull;

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;
}

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> T dup(T a, T b) { return (a + b - 1) / b; }
template <class T> T modpow(T x, T p, T m) {
  T a = 1;
  while (p) {
    if (p % 2)
      a = a * x % m;
    x = x + x % m;
    p /= 2;
  }
  return a;
}
// template <class T> void prim(T a) { for(ll i=0;i<a.size();++i) { cout <<
// a[i]; } cout << "\n"; }

#define mod 1000000007LL
#define INF 10000000000000000LL

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define rrep1(i, n) for (int i = n; i > 0; --i)

#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
// #define modpow(x, p, m) { ll a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2;
// } return a; }

#define m0(h) memset(h, 0, sizeof(h))
#define m1(h) memset(h, 1, sizeof(h))
#define m(h, n) memset(h, (n), sizeof(h))
#define all(n) (n).begin(), (n).end()
#define bit(n) (1LL << (n))
#define pb push_back

// #ifdef LOCAL
//
// #define dpri(...) fprintf(stderr, __VA_ARGS__)
// #define pria(g) rep(i,g.size()){ cout << i << "{"; rep(j,g[i].size()) cout <<
// g[i][j] << " "; cout << "}" << "\n"; } #else #define dpri(...) 42 #define
// pria(g) 42 #endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // ---------------------
  // コーディング開始
  // ---------------------
  ll n, r, m;
  cin >> n >> r >> m;
  ll city[r];
  rep(i, r) cin >> city[i];
  ll a, b, c;

  ll load[n][n];
  rep(i, n) rep(j, n) load[i][j] = INF;
  rep(i, n) load[i][i] = 0;
  rep(i, m) {
    cin >> a >> b >> c;
    load[a - 1][b - 1] = c;
    load[b - 1][a - 1] = c;
  }

  // 初期化
  rep(k, n) rep(i, n) rep(j, n) load[i][j] =
      min(load[i][j], load[i][k] + load[k][j]);

  ll ans = INF;
  sort(city, city + r);
  while (next_permutation(city, city + r)) {
    ll tempans = 0;
    rep(i, r - 1) tempans += load[city[i] - 1][city[i + 1] - 1];
    chmin(ans, tempans);
  }
  pri(ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
using gra = vector<vector<ll>>;
using gram = vector<unordered_map<ll, ll>>;

typedef unsigned long long ull;

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;
}

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> T dup(T a, T b) { return (a + b - 1) / b; }
template <class T> T modpow(T x, T p, T m) {
  T a = 1;
  while (p) {
    if (p % 2)
      a = a * x % m;
    x = x + x % m;
    p /= 2;
  }
  return a;
}
// template <class T> void prim(T a) { for(ll i=0;i<a.size();++i) { cout <<
// a[i]; } cout << "\n"; }

#define mod 1000000007LL
#define INF 10000000000000000LL

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define rrep1(i, n) for (int i = n; i > 0; --i)

#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
// #define modpow(x, p, m) { ll a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2;
// } return a; }

#define m0(h) memset(h, 0, sizeof(h))
#define m1(h) memset(h, 1, sizeof(h))
#define m(h, n) memset(h, (n), sizeof(h))
#define all(n) (n).begin(), (n).end()
#define bit(n) (1LL << (n))
#define pb push_back

// #ifdef LOCAL
//
// #define dpri(...) fprintf(stderr, __VA_ARGS__)
// #define pria(g) rep(i,g.size()){ cout << i << "{"; rep(j,g[i].size()) cout <<
// g[i][j] << " "; cout << "}" << "\n"; } #else #define dpri(...) 42 #define
// pria(g) 42 #endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // ---------------------
  // コーディング開始
  // ---------------------
  ll n, r, m;
  cin >> n >> m >> r;
  ll city[r];
  rep(i, r) cin >> city[i];
  ll a, b, c;

  ll load[n][n];
  rep(i, n) rep(j, n) load[i][j] = INF;
  rep(i, n) load[i][i] = 0;
  rep(i, m) {
    cin >> a >> b >> c;
    load[a - 1][b - 1] = c;
    load[b - 1][a - 1] = c;
  }

  // 初期化
  rep(k, n) rep(i, n) rep(j, n) load[i][j] =
      min(load[i][j], load[i][k] + load[k][j]);

  ll ans = INF;
  sort(city, city + r);
  while (next_permutation(city, city + r)) {
    ll tempans = 0;
    rep(i, r - 1) tempans += load[city[i] - 1][city[i + 1] - 1];
    chmin(ans, tempans);
  }
  pri(ans);

  return 0;
}