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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int index, boat, next, cost;
  node(int index, int boat, int next, int cost)
      : index(index), boat(boat), next(next), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};
struct edge {
  int lcost, scost;
  edge(int lcost, int scost) : lcost(lcost), scost(scost) {}
  edge() {}
};
const int MAX = INT_MAX / 10;
const int NONE = -1;

typedef vector<edge> ve;
typedef vector<ve> vve;
typedef vector<vvi> vvvi;
int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    static edge graph[200][200];
    REP(i, n) REP(j, n) {
      if (i == j) {
        graph[i][j] = edge(0, 0);
      } else {
        graph[i][j] = edge(MAX, MAX);
      }
    }
    REP(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      switch (sl) {
      case 'S':
        graph[x - 1][y - 1].scost = min(graph[x - 1][y - 1].scost, t);
        graph[y - 1][x - 1].scost = min(graph[y - 1][x - 1].scost, t);
        break;
      case 'L':
        graph[x - 1][y - 1].lcost = min(graph[x - 1][y - 1].lcost, t);
        graph[y - 1][x - 1].lcost = min(graph[y - 1][x - 1].lcost, t);
        break;
      default:
        assert(0);
        break;
      }
    }
    int r;
    cin >> r;
    vi order(r);
    REP(i, r) {
      cin >> order[i];
      order[i]--;
    }
    REP(k, n) REP(i, n) REP(j, n) {
      graph[i][j].lcost =
          min(graph[i][j].lcost, graph[i][k].lcost + graph[k][j].lcost);
      graph[i][j].scost =
          min(graph[i][j].scost, graph[i][k].scost + graph[k][j].scost);
    }

    static int dp[1001][200];
    REP(i, r + 1) {
      REP(j, n) { dp[i][j] = MAX; }
    }
    for (int post = -1; post < r - 1; post++) {
      REP(boat, n) {
        if (post == -1) {
          dp[post + 1][boat] = graph[order[0]][boat].scost;
        } else {
          dp[post + 1][boat] =
              min(dp[post + 1][boat],
                  dp[post][boat] + graph[order[post]][order[post + 1]].lcost);
          REP(via, n) {
            dp[post + 1][boat] =
                min(dp[post + 1][boat], dp[post][via] +
                                            graph[order[post]][via].lcost +
                                            graph[via][boat].scost +
                                            graph[boat][order[post + 1]].lcost);
          }
        }
      }
    }

    int ans = MAX;
    REP(i, n) { ans = min(ans, dp[r - 1][i]); }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int index, boat, next, cost;
  node(int index, int boat, int next, int cost)
      : index(index), boat(boat), next(next), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};
struct edge {
  int lcost, scost;
  edge(int lcost, int scost) : lcost(lcost), scost(scost) {}
  edge() {}
};
const int MAX = INT_MAX / 10;
const int NONE = -1;

typedef vector<edge> ve;
typedef vector<ve> vve;
typedef vector<vvi> vvvi;
int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    static edge graph[200][200];
    REP(i, n) REP(j, n) {
      if (i == j) {
        graph[i][j] = edge(0, 0);
      } else {
        graph[i][j] = edge(MAX, MAX);
      }
    }
    REP(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      switch (sl) {
      case 'S':
        graph[x - 1][y - 1].scost = min(graph[x - 1][y - 1].scost, t);
        graph[y - 1][x - 1].scost = min(graph[y - 1][x - 1].scost, t);
        break;
      case 'L':
        graph[x - 1][y - 1].lcost = min(graph[x - 1][y - 1].lcost, t);
        graph[y - 1][x - 1].lcost = min(graph[y - 1][x - 1].lcost, t);
        break;
      default:
        assert(0);
        break;
      }
    }
    int r;
    cin >> r;
    vi order(r);
    REP(i, r) {
      cin >> order[i];
      order[i]--;
    }
    REP(k, n) REP(i, n) REP(j, n) {
      graph[i][j].lcost =
          min(graph[i][j].lcost, graph[i][k].lcost + graph[k][j].lcost);
      graph[i][j].scost =
          min(graph[i][j].scost, graph[i][k].scost + graph[k][j].scost);
    }

    static int dp[1001][200];
    REP(i, r + 1) {
      REP(j, n) { dp[i][j] = MAX; }
    }
    dp[0][0] = 0;
    for (int post = -1; post < r - 1; post++) {
      REP(boat, n) {
        if (post == -1) {
          dp[post + 1][0] =
              min(dp[post + 1][0], graph[0][order[post + 1]].lcost);
          REP(vial, n) {
            REP(vias, n) {
              if (vial == vias)
                continue;
              dp[post + 1][vias] =
                  min(dp[post + 1][vias],
                      graph[0][vial].lcost + graph[vial][vias].scost +
                          graph[vias][order[post + 1]].lcost);
            }
          }
        } else {
          dp[post + 1][boat] =
              min(dp[post + 1][boat],
                  dp[post][boat] + graph[order[post]][order[post + 1]].lcost);
          REP(vial, n) {
            REP(vias, n) {
              if (vial == vias)
                continue;
              dp[post + 1][vias] =
                  min(dp[post + 1][vias],
                      dp[post][vial] + graph[order[post]][vial].lcost +
                          graph[vial][vias].scost +
                          graph[vias][order[post + 1]].lcost);
            }
          }
        }
      }
    }

    int ans = MAX;
    REP(i, n) { ans = min(ans, dp[r - 1][i]); }
    cout << ans << endl;
  }
}