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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const long long INF = 1LL << 60;
const int MOD = 1000000000 + 7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// cout << fixed << setprecision(10) << ans << endl;
// *min_element(c + l, c + r) *max_element(c + l, c + r)
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// struct Edge {
//   int to, id;
//   Edge(int to, int id): to(to), id(id) {}
// };

ll dp[3010][3010][4];

int field[3010][3010];

int seen[3010][3010];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b;
    ll d;
    cin >> a >> b >> d;
    a--;
    b--;
    field[a][b] = d;
  }
  queue<P> qu;
  qu.push(P(0, 0));
  dp[0][0][1] = field[0][0];
  while (!qu.empty()) {
    int y = qu.front().first;
    int x = qu.front().second;
    qu.pop();
    // cout<<y<<" "<<x<<endl;
    if (x + 1 <= c - 1) {
      rep(i, 3) {
        dp[y][x + 1][i + 1] =
            max(dp[y][x + 1][i + 1],
                max(dp[y][x][i + 1], dp[y][x][i] + field[y][x + 1]));
      }
      dp[y][x + 1][0] = max(dp[y][x + 1][0], dp[y][x][0]);
      seen[y][x + 1]++;
      if (seen[y][x + 1] == 2 || y == 0)
        qu.push(P(y, x + 1));
    }
    if (y + 1 <= r - 1) {
      rep(i, 4) dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][i]);
      rep(i, 4) dp[y + 1][x][1] =
          max(dp[y + 1][x][1], dp[y][x][i] + field[y + 1][x]);
      seen[y + 1][x]++;
      if (seen[y + 1][x] == 2 || x == 0)
        qu.push(P(y + 1, x));
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const long long INF = 1LL << 60;
const int MOD = 1000000000 + 7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// cout << fixed << setprecision(10) << ans << endl;
// *min_element(c + l, c + r) *max_element(c + l, c + r)
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// struct Edge {
//   int to, id;
//   Edge(int to, int id): to(to), id(id) {}
// };

ll dp[3010][3010][4];

int field[3010][3010];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b;
    ll d;
    cin >> a >> b >> d;
    a--;
    b--;
    field[a][b] = d;
  }
  queue<P> qu;
  qu.push(P(0, 0));
  dp[0][0][1] = field[0][0];
  while (!qu.empty()) {
    int y = qu.front().first;
    int x = qu.front().second;
    qu.pop();
    // cout<<y<<" "<<x<<endl;
    if (x + 1 <= c - 1) {
      rep(i, 3) {
        dp[y][x + 1][i + 1] =
            max(dp[y][x + 1][i + 1],
                max(dp[y][x][i + 1], dp[y][x][i] + field[y][x + 1]));
      }
      dp[y][x + 1][0] = max(dp[y][x + 1][0], dp[y][x][0]);
      qu.push(P(y, x + 1));
    }
    if (y + 1 <= r - 1) {
      rep(i, 4) dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][i]);
      rep(i, 4) dp[y + 1][x][1] =
          max(dp[y + 1][x][1], dp[y][x][i] + field[y + 1][x]);
      qu.push(P(y + 1, x));
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}