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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define inf (1 << 28)
#define ll long long
#define db double
#define c233 cout << "233" << endl
#define mem(s) memset(s, 0, sizeof(s))
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
const int N = 205;
const int M = 1050;
using namespace std;

int l[N][N], s[N][N], dp[M][N], way[M];
int n, m, r, ans;

void fre() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
}

inline int gi() {
  int x = 0, res = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      res *= -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0')
    x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
  return x * res;
}

void init() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      l[i][j] = s[i][j] = inf;
  for (int i = 1; i <= n; i++)
    l[i][i] = s[i][i] = 0;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    char d;
    scanf("%d%d%d %c", &a, &b, &c, &d);
    if (d == 'L')
      l[a][b] = l[b][a] = min(l[a][b], c);
    if (d == 'S')
      s[a][b] = s[b][a] = min(s[a][b], c);
  }
  r = gi();
  for (int i = 1; i <= r; i++)
    way[i] = gi();
}

void pre() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        l[i][j] = min(l[i][j], l[i][k] + l[k][j]);
        s[i][j] = min(s[i][j], s[i][k] + s[k][j]);
      }
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] = inf;
  ans = inf;
}

void work() {
  dp[1][way[1]] = 0;
  for (int i = 2; i <= r; i++)
    for (int j = 1; j <= n; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j] + l[way[i - 1]][way[i]]);
      for (int k = 1; k <= n; k++)
        dp[i][k] = min(dp[i][k], dp[i - 1][j] + l[way[i - 1]][j] + s[j][k] +
                                     l[k][way[i]]);
    }
  for (int i = 1; i <= n; i++)
    ans = min(ans, dp[r][i]);
  printf("%d\\n", ans);
}

int main() {
  // fre();
  while (1) {
    n = gi();
    m = gi();
    if (n == 0 && m == 0)
      break;
    init();
    pre();
    work();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define maxm 10010
#define maxn 210
#define mk make_pair
#define fi first
#define se second
using namespace std;
struct data {
  int nex, to, w, type;
} e[maxm * 2];
int head[maxn], edge = 0, dis[maxn][maxn], dis1[maxn][maxn];
bool vis[maxn][maxn];
queue<pair<int, int>> q;
inline void add(int from, int to, int w, int type) {
  e[++edge].nex = head[from];
  e[edge].to = to;
  e[edge].w = w;
  e[edge].type = type;
  head[from] = edge;
}
int main() {
  int n, m, k, x, y, z, now, ans;
  while (1) {
    memset(head, 0, sizeof(head)), edge = 0;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    char s[3];
    for (int i = 1; i <= m; i++)
      scanf("%d%d%d%s", &x, &y, &z, s), add(x, y, z, s[0] == 'S'),
          add(y, x, z, s[0] == 'S');
    scanf("%d", &k);
    memset(dis, 127 / 2, sizeof(dis));
    dis[1][1] = 0, vis[1][1] = 1, q.push(mk(1, 1));
    for (int i = 1; i <= k; i++) {
      scanf("%d", &now);
      if (i > 1)
        memcpy(dis, dis1, sizeof(dis));
      while (!q.empty()) {
        int u = q.front().fi, p = q.front().se;
        q.pop();
        vis[u][p] = 0;
        if (u == p) {
          for (int i = head[u]; i; i = e[i].nex)
            if (e[i].type == 1) {
              int v = e[i].to;
              if (dis[v][v] > dis[u][p] + e[i].w) {
                dis[v][v] = dis[u][p] + e[i].w;
                if (!vis[v][v])
                  vis[v][v] = 1, q.push(mk(v, v));
              }
            }
        }
        for (int i = head[u]; i; i = e[i].nex)
          if (e[i].type == 0) {
            int v = e[i].to;
            if (dis[v][p] > dis[u][p] + e[i].w) {
              dis[v][p] = dis[u][p] + e[i].w;
              if (!vis[v][p])
                vis[v][p] = 1, q.push(mk(v, p));
            }
          }
      }
      memset(dis1, 127 / 2, sizeof(dis1));
      for (int i = 1; i <= n; i++)
        if (dis[now][i] < 500000000)
          dis1[now][i] = dis[now][i], q.push(mk(now, i)), vis[now][i] = 1;
    }
    ans = 1000000000;
    for (int i = 1; i <= n; i++)
      if (dis[now][i] < ans)
        ans = dis[now][i];
    printf("%d\\n", ans);
  }
  return 0;
}