Problem p01317 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const int N = 205;
int n, m, x, y, t, r, z[1010], W[N][N], L[N][N];
ll dp[1010][N];
char op[4];

void floyd(int (*dp)[N]) {
  for (int k = 1; k <= n; k++) {
    dp[k][k] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (k != i && k != j && i != j) {
          if (dp[i][k] != INF && dp[k][j] != INF) {
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
          }
        }
      }
    }
  }
}

int main() {
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(W, INF, sizeof(W));
    memset(L, INF, sizeof(L));
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d%s", &x, &y, &t, op);
      if (op[0] == 'L') {
        L[x][y] = min(L[x][y], t);
        L[y][x] = L[x][y];
      } else {
        W[x][y] = min(W[x][y], t);
        W[y][x] = W[x][y];
      }
    }
    scanf("%d", &r);
    for (int i = 1; i <= r; i++)
      scanf("%d", z + i);
    floyd(L);
    floyd(W);
    for (int i = 1; i <= r; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = 1e18;
      }
    }
    for (int i = 1; i <= n; i++)
      dp[1][i] = W[z[1]][i] + L[z[1]][i];
    for (int i = 2; i <= r; i++) {
      for (int j = 1; j <= n; j++) {
        if (L[z[i - 1]][z[i]] != INF && dp[i - 1][j] != 1e18) {
          dp[i][j] = dp[i - 1][j] + L[z[i - 1]][z[i]];
        }
        for (int k = 1; k <= n; k++) {
          ll dis1 = L[z[i - 1]][k];
          ll dis2 = W[k][j];
          ll dis3 = L[j][z[i]];
          if (dis1 != INF && dis2 != INF && dis3 != INF &&
              dp[i - 1][k] != 1e18) {
            dp[i][j] = min(dp[i][j], dp[i - 1][k] + dis1 + dis2 + dis3);
          }
        }
      }
    }
    ll ans = 1e18;
    for (int i = 1; i <= n; i++)
      ans = min(ans, dp[r][i]);
    printf("%lld\\n", ans);
  }
}


Error: WA

Fixed submission:


#include <algorithm>
#include <assert.h>
#include <iostream>
#include <string.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const int N = 205;
int n, m, x, y, t, r, z[N], W[N][N], L[N][N], dp[1010][N];
char op[4];

void floyd(int (*dp)[N]) {
  for (int k = 1; k <= n; k++) {
    dp[k][k] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (k != i && k != j && i != j) {
          if (dp[i][k] != INF && dp[k][j] != INF) {
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
          }
        }
      }
    }
  }
}

int main() {
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(W, INF, sizeof(W));
    memset(L, INF, sizeof(L));
    memset(dp, INF, sizeof(dp));
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d%s", &x, &y, &t, op);
      if (op[0] == 'L') {
        L[x][y] = min(L[x][y], t);
        L[y][x] = L[x][y];
      } else {
        W[x][y] = min(W[x][y], t);
        W[y][x] = W[x][y];
      }
    }
    scanf("%d", &r);
    for (int i = 1; i <= r; i++)
      scanf("%d", z + i);
    // floyd(L);
    // floyd(W);
    dp[1][1] = 0;
    for (int i = 2; i <= n; i++)
      dp[1][i] = W[1][i];
    for (int i = 2; i <= r; i++) {
      for (int j = 1; j <= n; j++) {
        // if (L[z[i-1]][z[i]]!=INF&&dp[i-1][j]!=INF) {
        //     dp[i][j]=dp[i-1][j]+L[z[i-1]][z[i]];
        // }
        assert(z[i] < 5 * N);
        if (dp[i - 1][j] != INF) {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    int ans = INF;
    for (int i = 1; i <= n; i++)
      ans = min(ans, dp[r][i]);
    printf("%d\\n", ans);
  }
}