Problem p03664 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX
#define mod 1000000007

int n, m;
vector<P> g[100];
int sum[1 << 15];
int dp[1 << 15][15];
int tot = 0;

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].push_back(P(b, c));
    g[b].push_back(P(a, c));
    tot += c;
  }
  rep(S, 1 << n) {
    rep(i, n) {
      if (!((S >> i) & 1))
        continue;
      for (P p : g[i]) {
        if ((S >> p.fi) & 1)
          sum[S] += p.se;
      }
    }
    sum[S] /= 2;
  }

  rep(i, 1 << n) rep(j, n) dp[i][j] = -INF;
  dp[0][0] = 0;
  rep(S, 1 << n) rep(i, n) {
    if (dp[S][i] == -INF)
      continue;
    ll S_ = ((1 << n) - 1) ^ S;
    for (ll T = S_; T >= 0; T = (T - 1) & S_) {
      maxch(dp[S | T][i], dp[S][i] + sum[T]);
      for (P p : g[i]) {
        if (((T >> p.fi) & 1) || ((S >> p.fi) & 1))
          continue;
        maxch(dp[S | T | (1 << i)][p.fi], dp[S][i] + sum[T] + p.se);
      }
    }
  }

  int res = -INF;
  rep(S, 1 << n) maxch(res, dp[S][n - 1]);
  cout << tot - res << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX
#define mod 1000000007

int n, m;
vector<P> g[100];
int sum[1 << 15];
int dp[1 << 15][15];
int tot = 0;

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].push_back(P(b, c));
    g[b].push_back(P(a, c));
    tot += c;
  }
  rep(S, 1 << n) {
    rep(i, n) {
      if (!((S >> i) & 1))
        continue;
      for (P p : g[i]) {
        if ((S >> p.fi) & 1)
          sum[S] += p.se;
      }
    }
    sum[S] /= 2;
  }

  rep(i, 1 << n) rep(j, n) dp[i][j] = -INF;
  dp[0][0] = 0;
  rep(S, 1 << n) rep(i, n) {
    if (dp[S][i] == -INF)
      continue;
    ll S_ = ((1 << n) - 1) ^ S;
    for (ll T = S_; T >= 0; T--) {
      T &= S_;
      if ((T >> i) & 1)
        maxch(dp[S | (T ^ (1 << i))][i], dp[S][i] + sum[T]);
    }
    for (P p : g[i]) {
      if ((S >> p.fi) & 1)
        continue;
      maxch(dp[S | (1 << i)][p.fi], dp[S][i] + p.se);
    }
  }

  int res = -INF;
  rep(S, 1 << n) maxch(res, dp[S][n - 1]);
  cout << tot - res << endl;
  return 0;
}