Problem p01411 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

// bou height rest
ld dp[100][501][101];
int main() {
  for (int i = 0; i < 100; ++i) {
    for (int j = 0; j < 501; ++j) {
      for (int k = 0; k < 101; ++k) {
        dp[i][j][k] = 0;
      }
    }
  }
  int H, N, P, M, K;
  cin >> H >> N >> P >> M >> K;
  P--;
  vector<int> lines(H, -1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    lines[a] = b;
  }
  int undecidednum = H - M;
  dp[P][H][K] = 1;
  for (int h = H - 1; h >= 0; --h) {
    if (lines[h] != -1) {
      for (int rest = 0; rest <= K; ++rest) {
        for (int i = 0; i < N; ++i) {
          dp[i][h][rest] = dp[i][h + 1][rest];
        }
        swap(dp[lines[h]][h][rest], dp[lines[h] + 1][h][rest]);
      }
    } else {
      for (int rest = 0; rest <= K; ++rest) {
        if (rest > undecidednum) {

        } else {
          ld per = (ld(rest) / undecidednum) / (N - 1);

          {
            vector<ld> pluss(N);
            for (int sw = 0; sw < N - 1; ++sw) {

              for (int i = 0; i < N; ++i) {
                pluss[i] = dp[i][h + 1][rest] * per;
              }
              swap(pluss[sw], pluss[sw + 1]);
              for (int i = 0; i < N; ++i) {
                dp[i][h][rest - 1] += pluss[i];
              }
            }
          }
          {
            vector<ld> pluss(N);

            for (int i = 0; i < N; ++i) {
              pluss[i] = dp[i][h + 1][rest] * (1 - per * (N - 1));
            }
            for (int i = 0; i < N; ++i) {
              dp[i][h][rest] += pluss[i];
            }
          }
        }
      }
      undecidednum--;
    }
  }
  ld ans = 0;
  for (int i = 0; i < N; ++i) {
    ans = max(dp[i][0][0], ans);
  }
  cout << setprecision(22) << fixed << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
template <class T> using Table = vector<vector<T>>;

// bou height rest
ld dp[100][501][101];
int main() {
  for (int i = 0; i < 100; ++i) {
    for (int j = 0; j < 501; ++j) {
      for (int k = 0; k < 101; ++k) {
        dp[i][j][k] = 0;
      }
    }
  }
  int H, N, P, M, K;
  cin >> H >> N >> P >> M >> K;
  P--;
  vector<int> lines(H, -1);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    lines[a] = b;
  }
  int undecidednum = H - M;
  dp[P][H][K] = 1;
  for (int h = H - 1; h >= 0; --h) {
    if (lines[h] != -1) {
      for (int rest = 0; rest <= K; ++rest) {
        for (int i = 0; i < N; ++i) {
          dp[i][h][rest] = dp[i][h + 1][rest];
        }
        swap(dp[lines[h]][h][rest], dp[lines[h] + 1][h][rest]);
      }
    } else {
      for (int rest = 0; rest <= K; ++rest) {
        if (rest > undecidednum) {

        } else {
          ld per = (ld(rest) / undecidednum) / (N - 1);

          {
            vector<ld> pluss(N);
            for (int sw = 0; sw < N - 1; ++sw) {

              for (int i = 0; i < N; ++i) {
                pluss[i] = dp[i][h + 1][rest] * per;
              }
              swap(pluss[sw], pluss[sw + 1]);
              for (int i = 0; i < N; ++i) {
                dp[i][h][rest - 1] += pluss[i];
              }
            }
          }
          {
            vector<ld> pluss(N);

            for (int i = 0; i < N; ++i) {
              pluss[i] = dp[i][h + 1][rest] * (1 - per * (N - 1));
            }
            for (int i = 0; i < N; ++i) {
              dp[i][h][rest] += pluss[i];
            }
          }
        }
      }
      undecidednum--;
    }
  }
  ld ans = 0;
  for (int i = 0; i < N; ++i) {
    ans = max(dp[i][0][0], ans);
  }
  cout << setprecision(22) << fixed << ans << endl;

  return 0;
}