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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, m, s, g;
vector<int> et[40];
vector<string> ec[40];
string dp[481][40];

int main() {
  int i, j, k;

  while (cin >> n >> m >> s >> g) {
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      et[i].clear();
      ec[i].clear();
    }
    for (i = 0; i <= 12 * n; i++) {
      for (j = 0; j < n; j++) {
        dp[i][j] = "{";
      }
    }
    for (i = 0; i < m; i++) {
      int a, b;
      string spell;
      cin >> a >> b >> spell;
      et[a].push_back(b);
      ec[a].push_back(spell);
    }

    dp[0][s] = "";
    for (i = 0; i < 12 * n; i++) {
      for (j = 0; j < n; j++) {
        for (k = 0; k < et[j].size(); k++) {
          int v = et[j][k];
          int len = ec[j][k].length();
          if (i + len > 12 * n)
            continue;
          dp[i + len][v] = min(dp[i + len][v], dp[i][j] + ec[j][k]);
        }
      }
    }

    int id = 0;
    for (i = 1; i <= 12 * n; i++) {
      if (dp[id][g] > dp[i][g])
        id = i;
    }
    if (id >= 6 * n || dp[id][g] == "{") {
      cout << "NO" << endl;
    } else {
      cout << dp[id][g] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// dp[??????] := ?????????????°?, ??§??? [ab + c < a +
// c]??¨?????£???????????????????????§?????§??????????????????S1 + T < S2 + T
// ??? S1 < S2????????????????????????
//?????????|S1| =
//|S2|?????¨??????????¨????????????????????????£??????dp[??????][??????] :=
//?????????????°???¨????????°????????????????????±???????????????????????´??°??????????????§???dp[?????£??????????????°][??????][??????]??¨?????????
//????????????2n???????????????????????°?????????
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, m, s, g;
vector<int> et[40];
vector<string> ec[40];
string dp[40][721];

void doDP() {
  int i, j, k;

  dp[s][0] = "";
  while (true) {
    bool updateFlag = false;
    for (i = 0; i < n; i++) {
      for (j = 0; j <= 18 * n; j++) {
        for (k = 0; k < et[i].size(); k++) {
          int v = et[i][k];
          int nlen = j + ec[i][k].length();
          if (nlen > 18 * n)
            continue;

          // i --> v
          if (dp[v][nlen] > dp[i][j] + ec[i][k]) {
            dp[v][nlen] = dp[i][j] + ec[i][k];
            updateFlag = true;
            if (v == g && nlen >= 12 * n) {
              break;
            }
          }
        }
      }
    }
    if (!updateFlag)
      break;
  }
}

int main() {
  int i, j;

  while (cin >> n >> m >> s >> g) {
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      et[i].clear();
      ec[i].clear();
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j <= 18 * n; j++) {
        dp[i][j] = "{";
      }
    }

    for (i = 0; i < m; i++) {
      int a, b;
      string str;
      cin >> a >> b >> str;
      et[a].push_back(b);
      ec[a].push_back(str);
    }

    doDP();
    int len = 0;
    for (i = 1; i <= 18 * n; i++)
      if (dp[g][i] < dp[g][len])
        len = i;
    if (dp[g][len] == "{" || len >= 12 * n) {
      cout << "NO" << endl;
    } else {
      cout << dp[g][len] << endl;
    }
  }
  return 0;
}