Problem p03664 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int dp[(1 << 15) + 10][18];
int su[(1 << 15)];
int co[20][20];
int main() {
  int n, m, sum = 0, a, b, c;
  memset(co, 0, sizeof(co));
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b >> c;
    a--;
    b--;
    co[a][b] = co[b][a] = c;
    sum += c;
  }
  memset(su, 0, sizeof(su));
  rep(i, (1 << n)) rep(j, n) REP(k, j + 1, n) {
    if ((i & (1 << j)) && (i & (1 << k)))
      su[i] += co[j][k];
  }
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;
  rep(i, (1 << n)) rep(j, n) {
    if (dp[i][j] < 0)
      continue;
    rep(k, n) {
      if (i & (1 << k) || co[j][k] < 1)
        continue;
      dp[i + (1 << k)][k] = max(dp[i + (1 << k)][k], dp[i][j] + co[j][k]);
    }
    int x = (1 << n) - 1 - j;
    for (int y = x; y > 0; y = ((y - 1) & x)) {
      dp[i + y][j] = max(dp[i + y][j], dp[i][j] + su[y + (1 << j)]);
    }
  }
  cout << sum - dp[(1 << n) - 1][n - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int dp[(1 << 15) + 10][18];
int su[(1 << 15)];
int co[20][20];
int main() {
  int n, m, sum = 0, a, b, c;
  memset(co, 0, sizeof(co));
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b >> c;
    a--;
    b--;
    co[a][b] = co[b][a] = c;
    sum += c;
  }
  memset(su, 0, sizeof(su));
  rep(i, (1 << n)) rep(j, n) REP(k, j + 1, n) {
    if ((i & (1 << j)) && (i & (1 << k)))
      su[i] += co[j][k];
  }
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;
  rep(i, (1 << n)) rep(j, n) {
    if (dp[i][j] < 0)
      continue;
    rep(k, n) {
      if (i & (1 << k) || co[j][k] < 1)
        continue;
      dp[i + (1 << k)][k] = max(dp[i + (1 << k)][k], dp[i][j] + co[j][k]);
    }
    int x = (1 << n) - 1 - i;
    for (int y = x; y > 0; y = ((y - 1) & x)) {
      dp[i + y][j] = max(dp[i + y][j], dp[i][j] + su[y + (1 << j)]);
    }
  }
  cout << sum - dp[(1 << n) - 1][n - 1] << endl;
}