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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e15 : 1e9 + 10;

#include <complex>
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-7;

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
struct C {
  P c;
  double r;
  C(const P &c, double r) : c(c), r(r) {}
};
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
bool operator==(const P &a, const P &b) {
  return a.real() == b.real() && a.imag() == b.imag();
}
} // namespace std

double dot(P a, P b) { return real(conj(a) * b); }

P projection(L a, P p) {
  double t = dot(p - a[0], a[0] - a[1]) / norm(a[0] - a[1]);
  return a[0] + t * (a[0] - a[1]);
}

double distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); }

P turn(P p, double t) { return p * exp(P(.0, t * PI / 180.0)); }

vector<L> tangentCC(C a, C b) {
  if (a.r < b.r)
    swap(a, b);
  double d = abs(a.c - b.c);
  vector<L> l;
  if (d < EPS)
    return l;
  if (a.r + b.r < d - EPS) {
    double t = acos((a.r + b.r) / d);
    t = t * 180 / PI;
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t),
           b.c + turn(b.r / d * (a.c - b.c), t)));
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t),
           b.c + turn(b.r / d * (a.c - b.c), -t)));
  } else if (a.r + b.r < d + EPS) {
    P p = a.c + a.r / d * (b.c - a.c);
    l.pb(L(p, p + turn(b.c - a.c, 90)));
  }

  if (abs(a.r - b.r) < d - EPS) {
    double t1 = acos((a.r - b.r) / d);
    t1 = t1 * 180 / PI;
    double t2 = 180 - t1;
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t1),
           b.c + turn(b.r / d * (a.c - b.c), -t2)));
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t1),
           b.c + turn(b.r / d * (a.c - b.c), t2)));
  } else if (abs(a.r - b.r) < d + EPS) {
    P p = a.c + a.r / d * (b.c - a.c);
    l.pb(L(p, p + turn(b.c - a.c, 90)));
  }
  return l;
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi L(n, vi(n, inf));
    vvi S(n, vi(n, inf));
    rep(i, n) L[i][i] = S[i][i] = 0;
    rep(i, m) {
      ll a, b, c;
      char d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      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);
    }
    rep(k, n) rep(i, n) rep(j, n) {
      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]);
    }
    int R;
    cin >> R;
    vi r(R);
    rep(i, R) cin >> r[i];
    rep(i, R) r[i]--;
    vvi dp(R, vi(n, inf));
    dp[0][r[0]] = 0;
    rep(i, R - 1) rep(j, n) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + L[r[i]][r[i + 1]]);
      rep(k, n) {
        dp[i + 1][k] =
            min(dp[i + 1][k], dp[i][j] + L[r[i]][j] + S[j][k] + L[k][r[i + 1]]);
      }
    }
    //		rep(i,R){rep(j,n)cout<<" "<<dp[i][j];cout<<endl;}
    ll out = inf;
    rep(i, n) out = min(out, dp[R - 1][i]);
    cout << out << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e15 : 1e9 + 10;

#include <complex>
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-7;

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
struct C {
  P c;
  double r;
  C(const P &c, double r) : c(c), r(r) {}
};
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
bool operator==(const P &a, const P &b) {
  return a.real() == b.real() && a.imag() == b.imag();
}
} // namespace std

double dot(P a, P b) { return real(conj(a) * b); }

P projection(L a, P p) {
  double t = dot(p - a[0], a[0] - a[1]) / norm(a[0] - a[1]);
  return a[0] + t * (a[0] - a[1]);
}

double distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); }

P turn(P p, double t) { return p * exp(P(.0, t * PI / 180.0)); }

vector<L> tangentCC(C a, C b) {
  if (a.r < b.r)
    swap(a, b);
  double d = abs(a.c - b.c);
  vector<L> l;
  if (d < EPS)
    return l;
  if (a.r + b.r < d - EPS) {
    double t = acos((a.r + b.r) / d);
    t = t * 180 / PI;
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t),
           b.c + turn(b.r / d * (a.c - b.c), t)));
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t),
           b.c + turn(b.r / d * (a.c - b.c), -t)));
  } else if (a.r + b.r < d + EPS) {
    P p = a.c + a.r / d * (b.c - a.c);
    l.pb(L(p, p + turn(b.c - a.c, 90)));
  }

  if (abs(a.r - b.r) < d - EPS) {
    double t1 = acos((a.r - b.r) / d);
    t1 = t1 * 180 / PI;
    double t2 = 180 - t1;
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t1),
           b.c + turn(b.r / d * (a.c - b.c), -t2)));
    l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t1),
           b.c + turn(b.r / d * (a.c - b.c), t2)));
  } else if (abs(a.r - b.r) < d + EPS) {
    P p = a.c + a.r / d * (b.c - a.c);
    l.pb(L(p, p + turn(b.c - a.c, 90)));
  }
  return l;
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi L(n, vi(n, inf));
    vvi S(n, vi(n, inf));
    rep(i, n) L[i][i] = S[i][i] = 0;
    rep(i, m) {
      ll a, b, c;
      char d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      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);
    }
    rep(k, n) rep(i, n) rep(j, n) {
      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]);
    }
    int R;
    cin >> R;
    vi r(R);
    rep(i, R) cin >> r[i];
    rep(i, R) r[i]--;
    vvi dp(R, vi(n, inf));
    dp[r[0]][0] = 0;
    rep(i, R - 1) rep(j, n) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + L[r[i]][r[i + 1]]);
      rep(k, n) {
        dp[i + 1][k] =
            min(dp[i + 1][k], dp[i][j] + L[r[i]][j] + S[j][k] + L[k][r[i + 1]]);
      }
    }
    //		rep(i,R){rep(j,n)cout<<" "<<dp[i][j];cout<<endl;}
    ll out = inf;
    rep(i, n) out = min(out, dp[R - 1][i]);
    cout << out << endl;
  }
}