Problem p01679 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int, int> P;
const int MAX = 1000000007;

int dp[(1 << 18)][20];

int main() {
  int n, m, l, s, t;
  while (cin >> n >> m >> l >> s >> t && n) {
    int v[n + 1][n + 1];
    fill(v[0], v[n + 1], MAX);
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      v[x][y] = z;
      v[y][x] = z;
    }
    for (int i = 1; i <= n; i++)
      v[i][i] = 0;
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (v[i][k] != MAX && v[k][j] != MAX)
            v[i][j] = min(v[i][j], v[i][k] + v[k][j]);
        }
      }
    }
    P r[l + 1];
    for (int i = 0; i < l; i++)
      cin >> r[i].F >> r[i].S;
    r[l].F = s;
    r[l].S = 0;
    l++;

    fill(dp[0], dp[(1 << l)], MAX);
    dp[0][l - 1] = 0;
    /*
    for(int i=0; i<l; i++) {
      dp[(1<<i)][l-1]=v[r[i].F][s]+r[i].S;
      if(dp[(1<<i)][l-1]>t) dp[(1<<i)][l-1]=MAX;
    }
    */
    int ans = 0;
    for (int p = 0; p < (1 << l); p++) {
      for (int i = 0; i < l; i++) {
        if (dp[p][i] != MAX) {
          if (i == l - 1) {
            int cnt = 0;
            for (int j = 0; j < l - 1; j++) {
              if (p & (1 << j))
                cnt++;
            }
            if (cnt > ans)
              ans = cnt;
          }

          for (int j = 0; j < l; j++) {
            if (!(p & (1 << j))) {
              dp[p | (1 << j)][j] = min(dp[p | (1 << j)][j],
                                        dp[p][i] + v[r[j].F][r[i].F] + r[j].S);
              if (dp[p | (1 << j)][j] > t)
                dp[p | (1 << j)][j] = MAX;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int, int> P;
const int MAX = 1000000007;

int main() {
  int n, m, l, s, t;
  while (cin >> n >> m >> l >> s >> t && n) {
    int v[n + 1][n + 1];
    fill(v[0], v[n + 1], MAX);
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      v[x][y] = z;
      v[y][x] = z;
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (v[i][k] != MAX && v[k][j] != MAX)
            v[i][j] = min(v[i][j], v[i][k] + v[k][j]);
        }
      }
    }
    P r[l + 1];
    for (int i = 0; i < l; i++)
      cin >> r[i].F >> r[i].S;
    r[l].F = s;
    r[l].S = 0;
    l++;
    int dp[(1 << l)][l + 1];
    fill(dp[0], dp[(1 << l)], MAX);
    dp[0][l - 1] = 0;

    for (int i = 0; i < l - 1; i++) {
      dp[(1 << i)][l - 1] = v[r[i].F][s] + r[i].S;
      if (dp[(1 << i)][l - 1] > t)
        dp[(1 << i)][l - 1] = MAX;
    }

    int ans = 0;
    for (int p = 0; p < (1 << l); p++) {
      for (int i = 0; i < l; i++) {
        if (dp[p][i] != MAX) {
          int cnt = 0;
          for (int j = 0; j < l; j++) {
            if (p & (1 << j))
              cnt++;
          }
          if (cnt > ans)
            ans = cnt - 1;

          for (int j = 0; j < l; j++) {
            if (!(p & (1 << j))) {
              dp[p | (1 << j)][j] = min(dp[p | (1 << j)][j],
                                        dp[p][i] + v[r[j].F][r[i].F] + r[j].S);
              if (dp[p | (1 << j)][j] > t)
                dp[p | (1 << j)][j] = MAX;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}