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

#include <bits/stdc++.h>
using namespace std;

int N, S, T;
int A[102][102];
int W[102][102];
int Q[102];
const int INF = (1 << 29);

typedef pair<int, int> P;
const double EPS = 1e-10;
typedef vector<double> vec;
typedef vector<vec> mat; // Ax = b テ」ツ?ョAテ」ツ?ョテゥツδィテ・ツ按?

vec gauss_jordan(const mat &A, const vec &b) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i + 1; j < n; j++)
      if (fabs(B[j][i]) > fabs(B[pivot][i]))
        pivot = j;
    swap(B[i], B[pivot]);
    // ティツァツ」テ」ツ?古」ツ?ェテ」ツ??」ツ?凝」ツ??」ツ?づ」ツ?」テ」ツ?ヲテ」ツつづ、ツクツ?ヲツ?湘」ツ?ァテ」ツ?ッテ」ツ?ェテ」ツ??」ツ??
    if (fabs(B[i][i]) < EPS)
      return vec();

    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++)
      if (i != j)
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
  }
  vec x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];

  return x;
}

void wsf(mat &M, vec &V) {
  fill(W[0], W[N], INF);
  for (int i = 0; i < N; i++)
    W[i][i] = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      if (A[i][j])
        W[i][j] = A[i][j];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        W[j][k] = min(W[j][k], W[j][i] + W[i][k]);

  M = mat(N, vec(N));
  V = vec(N);
  for (int i = 0; i < N; i++) {
    int k = 0;
    int cs = 0;
    if (i == T) {
      M[i][i] = 1;
      continue;
    } else if (Q[i]) {
      for (int j = 0; j < N; j++)
        if (A[i][j] && W[i][T] == A[i][j] + W[j][T]) {
          M[i][j] = -1;
          k++;
          cs += A[i][j];
        }
    } else {
      for (int j = 0; j < N; j++)
        if (A[i][j]) {
          M[i][j] = -1;
          k++;
          cs += A[i][j];
        }
    }
    M[i][i] = k;
    V[i] = cs;
  }
}

int main() {
  while (cin >> N >> S >> T && (N | S | T)) {
    --S;
    --T;
    for (int i = 0; i < N; i++)
      cin >> Q[i];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        cin >> A[i][j];
    mat M;
    vec V;
    wsf(M, V);
    if (W[S][T] == INF)
      cout << "impossible" << endl;
    else
      printf("%.9lf\\n", gauss_jordan(M, V)[S]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define MAX (501)

struct edge {
  int to, c;
  bool operator<(const edge &e) const { return c > e.c; }
};

double dp[101][101][MAX];
int N, S, T, Q[101];
vector<edge> G[101];
vector<edge> nG[101];

int H[101];
void dijk() {
  fill(H, H + 101, (1 << 29));
  priority_queue<edge> q;
  q.push((edge){T, 0});
  H[T] = 0;
  while (!q.empty()) {
    edge p = q.top();
    q.pop();
    int id = p.to;
    int c = p.c;
    if (H[id] < c)
      continue;
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge e = G[id][i];
      if (H[e.to] > e.c + c) {
        H[e.to] = e.c + c;
        q.push((edge){e.to, e.c + c});
      }
    }
  }

  for (int i = 0; i < N; i++) {
    int mini = 1000000000;
    for (int j = 0; j < (int)G[i].size(); j++)
      mini = min(mini, H[G[i][j].to]);
    for (int j = 0; j < (int)G[i].size(); j++) {
      if (mini == H[G[i][j].to])
        nG[i].push_back(G[i][j]);
      //    if( !nG[i].empty() ) cout << i << " -> " << nG[i].back().to << endl;
    }
  }
}

double solve(int id, int pr, int co) {
  if (dp[id][pr][co] > -0.5)
    return dp[id][pr][co];
  if (id == T)
    return dp[id][pr][co] = 0.0f;
  if (co == MAX - 1)
    return dp[id][pr][co] = 0.0f;
  double ret = 0.0;
  if (Q[id] == 0) {
    double k = 1.0 / (double)(G[id].size() - 1);
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge e = G[id][i];
      if (e.to == pr)
        continue;
      ret += (solve(e.to, id, co + 1) + (double)e.c) * k;
    }
  } else {
    int cnt = 0;
    int mini = 100000000;
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge e = G[id][i];
      if (e.to == pr)
        continue;
      if (mini == H[e.to]) {
        cnt++;
      } else if (mini > H[e.to]) {
        mini = H[e.to];
        cnt = 1;
      }
    }
    double k = 1.0 / (double)cnt;
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge e = G[id][i];
      if (e.to == pr)
        continue;
      if (mini == H[e.to]) {
        cnt++;
        ret += (solve(e.to, id, co + 1) + (double)e.c) * k;
      }
    }
  }
  return dp[id][pr][co] = ret;
}

int main() {
  while (cin >> N >> S >> T && (S || T || N)) {
    --S;
    --T;
    for (int i = 0; i < N; i++)
      cin >> Q[i];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        int a;
        cin >> a;
        if (a != 0) {
          G[i].push_back((edge){j, a});
        }
      }
    }
    for (int i = 0; i < N; i++)
      for (int k = 0; k < N; k++)
        for (int j = 0; j < MAX; j++)
          dp[i][k][j] = -1.0f;
    dijk();
    double res = solve(S, S, 0);
    if (res < 1e-8)
      printf("impossible\\n");
    else
      printf("%.8lf\\n", res);
    for (int i = 0; i < N; i++) {
      G[i].clear();
      nG[i].clear();
    }
  }
}