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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Edge {
  int dst, w;
};

const int MAX = 110;
int V, E, sp, ep, iHIT;
vector<Edge> G[MAX];
int maxi[MAX];

void compute() {
  bool update = true;
  rep(i, V) maxi[i] = 0;
  vector<int> gotoMaxi, candidate;
  gotoMaxi.push_back(sp);
  while (update) {
    update = false;
    rep(i, (int)gotoMaxi.size()) maxi[gotoMaxi[i]] = iHIT;
    gotoMaxi.clear();
    candidate.clear();

    rep(i, V * 3 + 1) {
      rep(j, V) if (j != ep && maxi[j]) {
        rep(k, (int)G[j].size()) {
          Edge &e = G[j][k];
          if (maxi[e.dst] < min(maxi[j] + e.w, iHIT)) {
            update = true;
            maxi[e.dst] = min(maxi[j] + e.w, iHIT);
            if (i == V * 3)
              candidate.push_back(e.dst);
          }
        }
      }
    }

    int maxValue = 0;
    rep(i, (int)candidate.size()) maxValue = max(maxValue, maxi[candidate[i]]);
    if (maxValue)
      rep(i, (int)candidate.size()) if (maxValue == maxi[candidate[i]])
          gotoMaxi.push_back(candidate[i]);
  }

  /*
  rep(i,V) {
    cout << i << "-th : " << maxi[i] << endl;
  } puts("");
  */

  if (maxi[ep] == 0)
    puts("GAME OVER");
  else
    cout << maxi[ep] << endl;
}

int main() {
  int CNT = 1;
  while (cin >> V >> E, V | E) {
    rep(i, V) G[i].clear();
    rep(i, E) {
      int s, t, c;
      cin >> s >> t >> c;
      G[s].push_back((Edge){t, c});
    }
    cin >> sp >> ep >> iHIT;
    cout << "Case " << CNT++ << ": ";
    compute();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Edge {
  int dst, w;
};

const int MAX = 110;
const int UNDER = 0;
int V, E, sp, ep, iHIT;
vector<Edge> G[MAX];
int maxi[MAX];
int nmaxi[MAX];

/*
int newBeruman(int p){ // O(VE)
  rep(i,V) nmaxi[i] = UNDER;
  nmaxi[p] = 0;
  rep(i,V){
    rep(j,V)if( j != ep ){
      rep(k,(int)G[j].size()){
        Edge &e = G[j][k];
        if( nmaxi[j] != UNDER && nmaxi[e.dst] < min(nmaxi[j]+e.w,iHIT) ) {
          nmaxi[e.dst] = min(nmaxi[j]+e.w,iHIT);
        }
      }
    }
  }
  if( nmaxi[p] == UNDER ) return UNDER;
  return min(iHIT,nmaxi[p]);
}
*/

int newBeruman(vector<int> &sps) { // O(VE)
  rep(i, V) nmaxi[i] = UNDER;
  // nmaxi[p] = 0;
  rep(i, (int)sps.size()) nmaxi[sps[i]] = 0;
  rep(i, V) {
    rep(j, V) if (j != ep) {
      rep(k, (int)G[j].size()) {
        Edge &e = G[j][k];
        if (nmaxi[j] != UNDER && nmaxi[e.dst] < min(nmaxi[j] + e.w, iHIT)) {
          nmaxi[e.dst] = min(nmaxi[j] + e.w, iHIT);
        }
      }
    }
  }
  /*
  if( nmaxi[p] == UNDER ) return UNDER;
  return min(iHIT,nmaxi[p]);
  */
  rep(i, (int)sps.size()) {
    int p = sps[i];
    if (nmaxi[p] != UNDER) {
      maxi[p] = max(min(iHIT, nmaxi[p]), maxi[p]);
    }
  }
}

void compute() {
  rep(i, V) maxi[i] = UNDER;
  maxi[sp] = iHIT;
  bool update = true;
  int phase = 0;
  int presize = -1;
  int cnt = 0;
  while (update) { // O(VE)
    update = false;
    // vector<int> cyc;
    set<int> S;
    rep(i, V) if (i != ep) {
      rep(j, (int)G[i].size()) {
        Edge &e = G[i][j];
        if (maxi[i] != UNDER && maxi[e.dst] < min(maxi[i] + e.w, iHIT)) {
          maxi[e.dst] = min(maxi[i] + e.w, iHIT);
          update = true;
          if (phase >= V) {
            if (presize != 110)
              S.insert(e.dst);
            // cyc.push_back(e.dst);
            // int tmp = newBeruman(e.dst);
            // maxi[e.dst] = max(maxi[e.dst],tmp);
          }
        }
      }
    }

    if (S.size() && presize != 110 && presize == (int)S.size()) {
      presize = 110;
      ++cnt;
      vector<int> cyc;
      for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
        cyc.push_back(*it);
      }
      newBeruman(cyc);
    } else {
      presize = S.size();
    }

    /*
    if( cyc.size() ) {
      sort(cyc.begin(),cyc.end());
      cyc.erase(unique(cyc.begin(),cyc.end()),cyc.end());
      newBeruman(cyc);
    }
    */

    ++phase;
  }
  if (maxi[ep] == UNDER)
    puts("GAME OVER");
  else
    cout << maxi[ep] << endl;
}

int main() {
  int CNT = 1;
  while (cin >> V >> E, V | E) {
    rep(i, V) G[i].clear();
    rep(i, E) {
      int s, t, c;
      cin >> s >> t >> c;
      G[s].push_back((Edge){t, c});
    }
    cin >> sp >> ep >> iHIT;
    cout << "Case " << CNT++ << ": ";
    compute();
  }
  return 0;
}