Problem p01775 (C++)

Buggy submission (status: Runtime Error):
/* attention to overflow */
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define dump(x) cerr << #x << " = " << x << endl

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
/*printf("%.9Lf\n",cf);*/
const int MOD = 1000000007;

#define P pair<int, int>
const int V = 1010;

struct edge {
  int to, cost;
};
vector<edge> G[V];

int d[V];

void dijkstra(int src) {
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < V; i++) {
    d[i] = INF;
  }
  d[src] = 0;
  q.push(P(0, src));

  while (!q.empty()) {
    P s = q.top();
    q.pop();
    int v = s.second;
    if (s.first > d[v])
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P(d[e.to], e.to));
      }
    }
  }
}

int dp[1 << 14][14];

int main() {
  io();
  int n, m, k, p;
  cin >> n >> m >> k >> p;
  p--;
  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    G[x].push_back((edge){y, w});
    G[y].push_back((edge){x, w});
  }
  int a[15];
  int dist[10][10];

  for (int i = 0; i < k; i++) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    a[2 * i] = s;
    a[2 * i + 1] = t;
  }
  for (int i = 0; i < 2 * k; i++) {
    for (int j = i; j < 2 * k; j++) {
      if (i == j) {
        dist[i][j] = 0;
      } else {
        dijkstra(a[i]);
        dist[i][j] = d[a[j]];
        dist[j][i] = dist[i][j];
      }
    }
  }

  for (int i = 0; i < (1 << 14); i++) {
    fill_n(dp[i], 14, INF);
  }
  dijkstra(p);
  for (int i = 0; i < k; i++) {
    dp[1 << 2 * i][2 * i] = d[a[2 * i]];
  }
  for (int i = 2; i <= 2 * k; i++) {
    for (int j = 0; j < (1 << 2 * k); j++) {
      for (int u = 0; u < 2 * k; u++) {
        if ((j & (1 << u)) == 0)
          continue;
        for (int v = 0; v < k; v++) {
          int s = 2 * v;
          int t = s + 1;
          if ((j & (1 << s)) == 0) {
            dp[j | (1 << s)][s] =
                min(dp[j | (1 << s)][s], dp[j][u] + dist[u][s]);
          } else if ((j & (1 << t)) == 0) {
            dp[j | (1 << t)][t] =
                min(dp[j | (1 << t)][t], dp[j][u] + dist[u][t]);
          }
        }
      }
    }
  }

  int ret = INF;
  for (int i = 0; i < 2 * k; i++) {
    ret = min(ret, dp[(1 << (2 * k)) - 1][i]);
  }
  if (ret == INF) {
    cout << "Cannot deliver" << endl;
  } else {
    cout << ret << endl;
  }

  return 0;
}

Fixed submission:
/* attention to overflow */
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define dump(x) cerr << #x << " = " << x << endl

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
/*printf("%.9Lf\n",cf);*/
const int MOD = 1000000007;

#define P pair<int, int>
const int V = 1010;

struct edge {
  int to, cost;
};
vector<edge> G[V];

int d[V];

void dijkstra(int src) {
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < V; i++) {
    d[i] = INF;
  }
  d[src] = 0;
  q.push(P(0, src));

  while (!q.empty()) {
    P s = q.top();
    q.pop();
    int v = s.second;
    if (s.first > d[v])
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P(d[e.to], e.to));
      }
    }
  }
}

int dp[1 << 14][14];

int main() {
  io();
  int n, m, k, p;
  cin >> n >> m >> k >> p;
  p--;
  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    G[x].push_back((edge){y, w});
    G[y].push_back((edge){x, w});
  }
  int a[15];
  int dist[20][20];

  for (int i = 0; i < k; i++) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    a[2 * i] = s;
    a[2 * i + 1] = t;
  }
  for (int i = 0; i < 2 * k; i++) {
    for (int j = i; j < 2 * k; j++) {
      if (i == j) {
        dist[i][j] = 0;
      } else {
        dijkstra(a[i]);
        dist[i][j] = d[a[j]];
        dist[j][i] = dist[i][j];
      }
    }
  }

  for (int i = 0; i < (1 << 14); i++) {
    fill_n(dp[i], 14, INF);
  }
  dijkstra(p);
  for (int i = 0; i < k; i++) {
    dp[1 << 2 * i][2 * i] = d[a[2 * i]];
  }
  for (int i = 2; i <= 2 * k; i++) {
    for (int j = 0; j < (1 << 2 * k); j++) {
      for (int u = 0; u < 2 * k; u++) {
        if ((j & (1 << u)) == 0)
          continue;
        for (int v = 0; v < k; v++) {
          int s = 2 * v;
          int t = s + 1;
          if ((j & (1 << s)) == 0) {
            dp[j | (1 << s)][s] =
                min(dp[j | (1 << s)][s], dp[j][u] + dist[u][s]);
          } else if ((j & (1 << t)) == 0) {
            dp[j | (1 << t)][t] =
                min(dp[j | (1 << t)][t], dp[j][u] + dist[u][t]);
          }
        }
      }
    }
  }

  int ret = INF;
  for (int i = 0; i < 2 * k; i++) {
    ret = min(ret, dp[(1 << (2 * k)) - 1][i]);
  }
  if (ret == INF) {
    cout << "Cannot deliver" << endl;
  } else {
    cout << ret << endl;
  }

  return 0;
}