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

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q2;
vector<pair<int, int>> X[820], Y[820];
queue<int> Q;
int n, a[820], b[820], dist[820];
vector<pair<int, int>> p[820];
void BFS(int V, int s) {
  for (int i = 1; i <= V; i++) {
    dist[i] = 1 << 30;
  }
  dist[s] = 0;
  Q.push(s);
  while (!Q.empty()) {
    long long u = Q.front();
    Q.pop();
    for (int i = 0; i < X[u].size(); i++) {
      int to = X[u][i].first;
      if (dist[to] == 1 << 30) {
        dist[to] = dist[u] + 1;
        Q.push(to);
      }
    }
  }
}
void dijkstra(int V, int E, int s) {
  for (int i = 1; i <= V; i++) {
    dist[i] = 1 << 30;
  }
  dist[s] = 0;
  Q2.push(make_pair(0, s));
  int cnt = 0;
  while (!Q2.empty()) {
    pair<int, int> p = Q2.top();
    Q2.pop();
    for (int i = 0; i < Y[p.second].size(); i++) {
      int to = Y[p.second][i].first, leng = Y[p.second][i].second;
      if (dist[to] > dist[p.second] + leng) {
        dist[to] = dist[p.second] + leng;
        Q2.push(make_pair(dist[to], to));
      }
    }
  }
  return;
}
int main() {
  while (true) {
    for (int i = 0; i < 820; i++)
      X[i].clear();
    cin >> n;
    int S = 0;
    if (n == 0)
      break;
    for (int i = 2; i <= n; i++)
      cin >> a[i];
    for (int i = 2; i <= n; i++) {
      cin >> b[i];
      S += b[i];
      X[i].push_back(make_pair(a[i], b[i]));
      X[a[i]].push_back(make_pair(i, b[i]));
    }
    int P = 1 << 30;
    for (int i = 1; i <= n; i++) {
      BFS(n, i);
      int T = 0;
      for (int j = 0; j < 820; j++) {
        p[j].clear();
        Y[j].clear();
      }
      for (int j = 1; j <= n; j++) {
        for (int k = 0; k < X[j].size(); k++) {
          if (dist[j] > dist[X[j][k].first])
            p[X[j][k].first].push_back(make_pair(j, X[j][k].second));
        }
      }
      for (int j = 1; j <= n; j++) {
        if (p[j].size() >= 1) {
          for (int k = 0; k < p[j].size(); k++) {
            int to = p[j][k].first;
            if (p[to].size() == 0)
              continue;
            Y[j].push_back(make_pair(to, p[j][k].second));
            Y[to].push_back(make_pair(j, p[j][k].second));
          }
        } else {
          for (int k = 0; k < X[j].size(); k++) {
            T += X[j][k].second;
          }
        }
      }
      int maxn = 0, mn = 0, mID = 0;
      dijkstra(n, n - 1, i);
      for (int i = 1; i <= n; i++) {
        if (p[i].size() == 0)
          continue;
        if (mn < dist[i]) {
          mn = dist[i];
          mID = i;
        }
      }
      if (mID == 0)
        goto E;
      dijkstra(n, n - 1, mID);
      for (int i = 1; i <= n; i++) {
        if (p[i].size() == 0)
          continue;
        maxn = max(maxn, dist[i]);
      }
    E:;
      P = min(P, S * 3 - T * 2 - maxn);
    }
    cout << P << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q2;
vector<pair<int, int>> X[820], Y[820];
queue<int> Q;
int n, a[820], b[820], dist[820];
vector<pair<int, int>> p[820];
void BFS(int V, int s) {
  for (int i = 1; i <= V; i++) {
    dist[i] = 1 << 30;
  }
  dist[s] = 0;
  Q.push(s);
  while (!Q.empty()) {
    long long u = Q.front();
    Q.pop();
    for (int i = 0; i < X[u].size(); i++) {
      int to = X[u][i].first;
      if (dist[to] == 1 << 30) {
        dist[to] = dist[u] + 1;
        Q.push(to);
      }
    }
  }
}
void dijkstra(int V, int E, int s) {
  for (int i = 1; i <= V; i++) {
    dist[i] = 1 << 30;
  }
  dist[s] = 0;
  Q2.push(make_pair(0, s));
  int cnt = 0;
  while (!Q2.empty()) {
    pair<int, int> p = Q2.top();
    Q2.pop();
    for (int i = 0; i < Y[p.second].size(); i++) {
      int to = Y[p.second][i].first, leng = Y[p.second][i].second;
      if (dist[to] > dist[p.second] + leng) {
        dist[to] = dist[p.second] + leng;
        Q2.push(make_pair(dist[to], to));
      }
    }
  }
  return;
}
int main() {
  while (true) {
    for (int i = 0; i < 820; i++)
      X[i].clear();
    cin >> n;
    int S = 0;
    if (n == 0)
      break;
    for (int i = 2; i <= n; i++)
      cin >> a[i];
    for (int i = 2; i <= n; i++) {
      cin >> b[i];
      S += b[i];
      X[i].push_back(make_pair(a[i], b[i]));
      X[a[i]].push_back(make_pair(i, b[i]));
    }
    int P = 1 << 30;
    for (int i = 1; i <= n; i++) {
      BFS(n, i);
      int T = 0;
      for (int j = 0; j < 820; j++) {
        p[j].clear();
        Y[j].clear();
      }
      for (int j = 1; j <= n; j++) {
        for (int k = 0; k < X[j].size(); k++) {
          if (dist[j] > dist[X[j][k].first])
            p[X[j][k].first].push_back(make_pair(j, X[j][k].second));
        }
      }
      for (int j = 1; j <= n; j++) {
        if (p[j].size() >= 1) {
          for (int k = 0; k < p[j].size(); k++) {
            int to = p[j][k].first;
            if (p[to].size() == 0)
              continue;
            Y[j].push_back(make_pair(to, p[j][k].second));
            Y[to].push_back(make_pair(j, p[j][k].second));
          }
        } else {
          for (int k = 0; k < X[j].size(); k++) {
            T += X[j][k].second;
          }
        }
      }
      int maxn = 0;
      for (int j = 1; j <= n; j++) {
        if (p[j].size() == 0)
          continue;
        dijkstra(n, n - 1, j);
        for (int k = 1; k <= n; k++) {
          if (p[k].size() == 0)
            continue;
          maxn = max(maxn, dist[k]);
        }
      }
      P = min(P, S * 3 - T * 2 - maxn);
    }
    cout << P << endl;
  }
  return 0;
}