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

#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
int dp1[160][160];
int rev[160][160];
int dp2[160][160];
int g[160][160];
int dp3[160][160];
int sg[160][160];
int cost[310][160];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  for (int i = 0; i < a; i++)
    for (int j = 0; j < a; j++) {
      g[i][j] = -99999999;
    }
  for (int i = 0; i < b; i++) {
    int p, q, r;
    scanf("%d%d%d", &p, &q, &r);
    if (!~g[p][q] || g[p][q] < r) {
      g[p][q] = r;
    }
  }
  for (int i = 0; i < 160; i++)
    for (int j = 0; j < 160; j++) {
      dp1[i][j] = dp2[i][j] = -999999999;
    }
  for (int i = 0; i < a; i++)
    dp1[0][i] = dp2[0][i] = 0;
  for (int i = 0; i < 155; i++) {
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        if (dp1[i][j] >= 0) {
          if (dp1[i + 1][k] < dp1[i][j] + g[j][k]) {
            dp1[i + 1][k] = dp1[i][j] + g[j][k];
            rev[i + 1][k] = j;
          }
        }
        if (dp2[i][j] >= 0) {
          dp2[i + 1][k] = max(dp2[i + 1][k], dp2[i][j] + g[k][j]);
        }
      }
    }
  }
  for (int i = 1; i <= 100; i++) {
    int ms = 0;
    int at = 0;
    for (int j = 0; j < a; j++) {
      if (ms < dp1[i][j]) {
        ms = dp1[i][j];
        at = j;
      }
    }
    if (ms >= c) {
      printf("%d\\n", i);
      stack<int> S;
      for (int j = i; j >= 0; j--) {
        S.push(at);
        at = rev[j][at];
      }
      for (int j = 0; j <= i; j++) {
        if (j)
          printf(" ");
        printf("%d", S.top());
        S.pop();
      }
      printf("\\n");
      return 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < 160; j++)
      for (int k = 0; k < 160; k++)
        sg[j][k] = -999999999;
    sg[0][i] = 0;
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        if (sg[j][k] < 0)
          continue;
        for (int l = 0; l < a; l++) {
          sg[j + 1][l] = max(sg[j + 1][l], sg[j][k] + g[k][l]);
        }
      }
    }
    for (int j = 0; j <= a; j++)
      dp3[i][j] = sg[j][i];
  }
  int ret = 99999999;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        for (int l = 0; l <= a; l++) {
          int t = dp1[i][j] + dp2[k][j];
          if (c <= t) {
            ret = min(ret, i + k);
            continue;
          }
          if (dp3[j][l] <= 0)
            continue;
          ret = min(ret, i + k + ((c - t) + dp3[j][l] - 1) / dp3[j][l] * l);
        }
      }
    }
  }
  if (ret > 9999999) {
    ret = -1;
  }
  printf("%d\\n", ret);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
int dp1[160][160];
int rev[160][160];
int dp2[160][160];
int g[160][160];
int dp3[160][160];
int sg[160][160];
int cost[310][160];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  for (int i = 0; i < a; i++)
    for (int j = 0; j < a; j++) {
      g[i][j] = -1;
    }
  for (int i = 0; i < b; i++) {
    int p, q, r;
    scanf("%d%d%d", &p, &q, &r);
    if (!~g[p][q] || g[p][q] < r) {
      g[p][q] = r;
    }
  }
  for (int i = 0; i < 160; i++)
    for (int j = 0; j < 160; j++) {
      dp1[i][j] = dp2[i][j] = -99999999;
    }
  for (int i = 0; i < a; i++)
    dp1[0][i] = dp2[0][i] = 0;
  for (int i = 0; i < 155; i++) {
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        if (~g[j][k] && dp1[i][j] >= 0) {
          if (dp1[i + 1][k] < dp1[i][j] + g[j][k]) {
            dp1[i + 1][k] = dp1[i][j] + g[j][k];
            rev[i + 1][k] = j;
          }
        }
        if (~g[k][j] && dp2[i][j] >= 0) {
          dp2[i + 1][k] = max(dp2[i + 1][k], dp2[i][j] + g[k][j]);
        }
      }
    }
  }
  for (int i = 1; i <= 100; i++) {
    int ms = 0;
    int at = 0;
    for (int j = 0; j < a; j++) {
      if (ms < dp1[i][j]) {
        ms = dp1[i][j];
        at = j;
      }
    }
    if (ms >= c) {
      printf("%d\\n", i);
      stack<int> S;
      for (int j = i; j >= 0; j--) {
        S.push(at);
        at = rev[j][at];
      }
      for (int j = 0; j <= i; j++) {
        if (j)
          printf(" ");
        printf("%d", S.top());
        S.pop();
      }
      printf("\\n");
      return 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < 160; j++)
      for (int k = 0; k < 160; k++)
        sg[j][k] = -999999999;
    sg[0][i] = 0;
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        if (sg[j][k] < 0)
          continue;
        for (int l = 0; l < a; l++) {
          if (!~g[k][l])
            continue;
          sg[j + 1][l] = max(sg[j + 1][l], sg[j][k] + g[k][l]);
        }
      }
    }
    for (int j = 0; j <= a; j++)
      dp3[i][j] = sg[j][i];
  }
  int ret = 99999999;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        for (int l = 0; l <= a; l++) {
          int t = dp1[i][j] + dp2[k][j];
          if (c <= t) {
            ret = min(ret, i + k);
            continue;
          }
          if (dp3[j][l] <= 0)
            continue;
          ret = min(ret, i + k + ((c - t) + dp3[j][l] - 1) / dp3[j][l] * l);
        }
      }
    }
  }
  if (ret > 9999999) {
    ret = -1;
  }
  printf("%d\\n", ret);
}