Problem p03608 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <regex>

/* g++ -g -std=c++0x */
/* g++ -g -std=c++11 */

using namespace std;

// std::ios::sync_with_stdio(false);

// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

const int inf = 1e9;
const int maxn = 210;
const int maxm = 1 << 10;
int G[maxn][maxn], dist[maxm][maxn];
int N, M, R, r[maxn];
int ans;

void dfs(const int mask, const int u) {
  if (mask + 1 == (1 << (R + 1))) {
    ans = min(ans, dist[mask][u]);
    return;
  }
  FOR(v, 1, N + 1) {
    int m = mask;
    if (r[v])
      m |= (1 << r[v]);
    if (dist[m][v] > dist[mask][u] + G[u][v]) {
      dist[m][v] = dist[mask][u] + G[u][v];
      dfs(m, v);
    }
  }
}

int main() {
  /* #ifndef ONLINE_JUDGE */
  /*     freopen("foo", "rt", stdin); */
  /* #endif */
  while (cin >> N >> M >> R) {
    memset(r, 0, sizeof(r));
    int cnt = 1;
    FOR(i, 0, R) {
      int t;
      cin >> t;
      r[t] = cnt++;
    }
    FOR(i, 0, N + 1) FOR(j, 0, N + 1) G[i][j] = inf;
    FOR(_, 0, M) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a][b] = G[b][a] = c;
    }
    FOR(k, 1, N + 1)
    FOR(i, 1, N + 1) FOR(j, 1, N + 1) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    ans = inf;
    FOR(u, 1, N + 1) if (r[u]) {
      FOR(i, 0, maxm) FOR(j, 0, maxn) dist[i][j] = inf;
      const int mask = (1 << 0) | (1 << r[u]);
      dist[mask][u] = 0;
      dfs(mask, u);
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <regex>

/* g++ -g -std=c++0x */
/* g++ -g -std=c++11 */

using namespace std;

// std::ios::sync_with_stdio(false);

// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

const int inf = 1e9;
const int maxn = 210;
const int maxm = 1 << 10;
int G[maxn][maxn], dist[maxm][maxn];
int N, M, R, r[maxn];
int ans;

void dfs(const int mask, const int u) {
  if (mask + 1 == (1 << (R + 1))) {
    ans = min(ans, dist[mask][u]);
    return;
  }
  FOR(v, 1, N + 1) if (r[v]) {
    int m = mask | (1 << r[v]);
    if (dist[m][v] > dist[mask][u] + G[u][v]) {
      dist[m][v] = dist[mask][u] + G[u][v];
      dfs(m, v);
    }
  }
}

int main() {
  /* #ifndef ONLINE_JUDGE */
  /*     freopen("foo", "rt", stdin); */
  /* #endif */
  while (cin >> N >> M >> R) {
    memset(r, 0, sizeof(r));
    int cnt = 1;
    FOR(i, 0, R) {
      int t;
      cin >> t;
      r[t] = cnt++;
    }
    FOR(i, 0, N + 1) FOR(j, 0, N + 1) G[i][j] = inf;
    FOR(_, 0, M) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a][b] = G[b][a] = c;
    }
    FOR(k, 1, N + 1)
    FOR(i, 1, N + 1) FOR(j, 1, N + 1) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    ans = inf;
    FOR(u, 1, N + 1) if (r[u]) {
      FOR(i, 0, maxm) FOR(j, 0, maxn) dist[i][j] = inf;
      const int mask = (1 << 0) | (1 << r[u]);
      dist[mask][u] = 0;
      dfs(mask, u);
    }
    cout << ans << endl;
  }
  return 0;
}