Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fst first
#define snd second
#define mp make_pair
using namespace std;

typedef long long ll;

const int maxn = 3e3 + 10;

const ll inf = 1e18 + 10;

const int dx[] = {1, 0};
const int dy[] = {0, 1};

ll dp[maxn][maxn][4], n, m, k;
ll cost[maxn][maxn];
priority_queue<pair<ll, pair<pair<int, int>, int>>> que;

bool check(int x, int y) {
  if (x >= n || y >= m)
    return false;
  return true;
}

int main() {
  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < maxn; j++)
      for (int c = 0; c < 4; c++)
        dp[i][j][k] = -inf;
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> cost[x][y];
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = cost[0][0];
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++)
      for (int c = 0; c < 4; c++) {
        for (int d = 0; d < 2; d++) {
          int nx = x + dx[d], ny = y + dy[d];
          dp[nx][ny][c] = max(dp[x][y][c], dp[nx][ny][c]);
          if (nx == x + 1)
            dp[nx][ny][0] = max(dp[nx][ny][0], dp[x][y][c]);
          if (c + 1 <= 3)
            dp[nx][ny][c + 1] =
                max(dp[nx][ny][c + 1], dp[x][y][c] + cost[nx][ny]);
          if (nx == x + 1)
            dp[nx][ny][1] = max(dp[x][y][c] + cost[nx][ny], dp[nx][ny][1]);
        }
      }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[n - 1][m - 1][i]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fst first
#define snd second
#define mp make_pair
using namespace std;

typedef long long ll;

const int maxn = 3e3 + 10;

const ll inf = 1e18 + 10;

const int dx[] = {1, 0};
const int dy[] = {0, 1};

ll dp[maxn][maxn][4], n, m, k;
ll cost[maxn][maxn];
priority_queue<pair<ll, pair<pair<int, int>, int>>> que;

bool check(int x, int y) {
  if (x >= n || y >= m)
    return false;
  return true;
}

int main() {
  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < maxn; j++)
      for (int c = 0; c < 4; c++)
        dp[i][j][k] = -inf;
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> cost[x][y];
  }
  dp[0][0][0] = 0;
  que.push(mp(dp[0][0][0], mp(mp(0, 0), 0)));
  dp[0][0][1] = cost[0][0];
  que.push(mp(dp[0][0][1], mp(mp(0, 0), 1)));
  while (!que.empty()) {
    ll val = que.top().fst;
    int x = que.top().snd.fst.fst, y = que.top().snd.fst.snd;
    int c = que.top().snd.snd;
    que.pop();
    if (val < dp[x][y][c])
      continue;
    //	cout<<x<<" "<<y<<" "<<c<<": "<<dp[x][y][c]<<endl;
    for (int i = 0; i < 2; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (check(nx, ny)) {
        if (dp[nx][ny][c] < dp[x][y][c]) {
          dp[nx][ny][c] = dp[x][y][c];
          que.push(mp(dp[nx][ny][c], mp(mp(nx, ny), c)));
        }
        if (nx == x + 1 && dp[nx][ny][0] < dp[x][y][c]) {
          dp[nx][ny][0] = dp[x][y][c];
          que.push(mp(dp[nx][ny][0], mp(mp(nx, ny), 0)));
        }
        if (c + 1 <= 3 && dp[nx][ny][c + 1] < dp[x][y][c] + cost[nx][ny]) {
          dp[nx][ny][c + 1] = dp[x][y][c] + cost[nx][ny];
          que.push(mp(dp[nx][ny][c + 1], mp(mp(nx, ny), c + 1)));
        }
        if (nx == x + 1 && dp[nx][ny][1] < dp[x][y][c] + cost[nx][ny]) {
          dp[nx][ny][1] = dp[x][y][c] + cost[nx][ny];
          que.push(mp(dp[nx][ny][1], mp(mp(nx, ny), 1)));
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[n - 1][m - 1][i]);
  cout << ans << endl;
  return 0;
}