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

// ???????????£???????????????
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

const int INF = 0xfffffff;

struct ST {
  ll d;
  int z, b, n;
  // priority_queue??¨  const???????????????
  bool operator<(const ST &a) const { return (d > a.d); }
};

int N, M, R;
int v[2][205][205];
int Z[1005];
int dp[1005][205]; // z, boat
int ans[205];      // boat

int solve() {
  priority_queue<ST> que;
  ll d, z, b, n;

  Fill(dp, INF);
  dp[0][Z[0]] = 0;
  Fill(ans, INF);
  ans[Z[0]] = 0;
  rep(z, R - 1) {
    rep(b, N) {
      chmin(dp[z + 1][b], dp[z][b] + v[0][Z[z]][Z[z + 1]]);
      rep(nb, N) {
        chmin(dp[z + 1][nb],
              dp[z][b] + v[0][Z[z]][b] + v[1][b][nb] + v[0][nb][Z[z + 1]]);
      }
    }
    // rep(b,N) cout << "dp[" << z+1 << "][" << b << "] = " << dp[z+1][b] <<
    // endl;
    continue;
    rep(b, N) if (ans[b] < INF) { que.push(ST{ans[b], z, b, Z[z]}); }
    Fill(dp, INF);
    Fill(ans, INF);
    while (!que.empty()) {
      ST st = que.top();
      que.pop();
      d = st.d;
      // z = st.z;
      b = st.b;
      n = st.n;
      if (dp[b][n] < d)
        continue;
      dp[b][n] = d;
      //			cout << "hit " << d << " " << z << " " << b << "
      //" << n << endl;
      rep(k, 2) if (!k || b == n) {
        //	for(auto _v: v[k][n]){
        //		int to = _v.first;
        //		ll cost = dp[b][n] + _v.second;
        //		int nz = z, nb = b;
        //		if( k ) nb = to;
        //		//if( to == Z[z+1] ) nz++;
        //		if( to == Z[z+1] ){
        //			chmin( ans[nb], cost );
        //		}else if( chmin(dp[nb][to], cost) ){
        //			que.push( ST{ cost, nz, nb, to } );
        //		}
        //	}
      }
    }
  }
  int ret = INF;
  rep(b, N) {
    // chmin( ret, ans[b] );
    chmin(ret, dp[R - 1][b]);
  }
  return ret;
}

int main() {
  int x, y, t;
  char sl;

  while (cin >> N >> M, N | M) {
    Fill(v, INF);
    rep(i, M) {
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      if (sl == 'L') {
        v[0][x][y] = t;
        v[0][y][x] = t;
      } else {
        v[1][x][y] = t;
        v[1][y][x] = t;
      }
    }
    cin >> R;
    rep(i, R) {
      cin >> Z[i];
      Z[i]--;
    }

    rep(l, 2) rep(i, N) v[l][i][i] = 0;
    rep(l, 2) rep(k, N) rep(i, N) rep(j, N) {
      chmin(v[l][i][j], v[l][i][k] + v[l][k][j]);
    }

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

const int INF = 0x3fffffff;

struct ST {
  ll d;
  int z, b, n;
  // priority_queue??¨  const???????????????
  bool operator<(const ST &a) const { return (d > a.d); }
};

int N, M, R;
vector<P> v[2][205];
int Z[1005];
ll dp[205][205]; // boat, n
ll ans[205];     // boat

ll solve() {
  priority_queue<ST> que;
  ll d, z, b, n;
  // dp[ Z[0] ][ Z[0] ] = 0;
  ans[Z[0]] = 0;
  rep(i, R - 1) {
    rep(b, N) if (ans[b] < INF) { que.push(ST{ans[b], i, b, Z[i]}); }
    Fill(dp, INF);
    Fill(ans, INF);
    while (!que.empty()) {
      ST st = que.top();
      que.pop();
      d = st.d;
      z = st.z;
      b = st.b;
      n = st.n;
      if (dp[b][n] < d)
        continue;
      dp[b][n] = d;
      // cout << "hit " << d << " " << z << " " << b << " " << n << endl;
      rep(k, 2) if (!k || b == n) {
        for (auto _v : v[k][n]) {
          int to = _v.first;
          ll cost = _v.second;
          int nz = z, nb = b;
          if (to == Z[z + 1])
            nz++;
          if (k)
            nb = to;
          if (chmin(dp[nb][to], dp[b][n] + cost)) {
            ll nd = dp[b][n] + cost;
            if (nz == z) {
              que.push(ST{nd, nz, nb, to});
            } else {
              chmin(ans[nb], nd);
            }
          }
        }
      }
    }
  }
  ll ret = INF;
  rep(b, N) { chmin(ret, ans[b]); }
  return ret;
}

int main() {
  int x, y, t;
  char sl;

  while (cin >> N >> M, N | M) {
    rep(i, 2) rep(j, 205) v[i][j].clear();
    Fill(dp, INF);
    Fill(ans, INF);
    rep(i, M) {
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      if (sl == 'L') {
        v[0][x].emplace_back(y, t);
        v[0][y].emplace_back(x, t);
      } else {
        v[1][x].emplace_back(y, t);
        v[1][y].emplace_back(x, t);
      }
    }
    cin >> R;
    rep(i, R) {
      cin >> Z[i];
      Z[i]--;
    }

    cout << solve() << endl;
  }

  return 0;
}