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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
long long int INF = 1000000000LL;
vector<long long int> dijkstra(int s, int v,
                               vector<vector<pair<long long int, int>>> G) {
  // s: ?§????, v: ????????°, G: ??£??\\?????????
  priority_queue<pair<long long int, int>> wait;
  vector<long long int> result(v, INF);
  result[s] = 0LL;
  wait.push(make_pair(0LL, s));
  while (!wait.empty()) {
    int nowpoint = wait.top().second;
    int nowcost = wait.top().first;
    wait.pop();
    if (nowcost <= result[nowpoint]) {
      for (int i = 0; i < G[nowpoint].size(); ++i) {
        int nextpoint = G[nowpoint][i].second;
        int nextcost = -nowcost + G[nowpoint][i].first;
        if (nextcost < result[nextpoint]) {
          wait.push(make_pair(-nextcost, nextpoint));
          result[nextpoint] = nextcost;
        }
      }
    }
  }
  return result;
}

int main() {
  int n;
  while (cin >> n, n) {
    long long int ans = 0LL;
    /*
    long long int G[n][n];
    for(int i = 0; i < n; ++i)
      fill(G[i],G[i]+n,INF);
    for(int i = 0; i < n; ++i)
      G[i][i] = 0;
    */
    int p[n], deg[n];
    fill(deg, deg + n, -1);
    for (int i = 1; i < n; ++i) {
      cin >> p[i];
      --p[i];
      ++deg[p[i]];
      ++deg[i];
    }
    long long int d[n];
    for (int i = 1; i < n; ++i) {
      cin >> d[i];
      if (deg[i] && deg[p[i]])
        ans += 3 * d[i];
      else
        ans += d[i];
    }
    vector<vector<pair<long long int, int>>> G(n);
    for (int i = 1; i < n; ++i) {
      if (deg[i] && deg[p[i]]) {
        G[i].push_back(make_pair(d[i], p[i]));
        G[p[i]].push_back(make_pair(d[i], i));
      }
    }
    int s = -1;
    for (int i = 0; i < n; ++i) {
      if (deg[i])
        s = i;
    }
    vector<long long int> D = dijkstra(s, n, G);
    long long int M = 0LL;
    for (int i = 0; i < n; ++i) {
      if (D[i] < INF && D[i] > M) {
        s = i;
        M = D[i];
      }
    }
    D = dijkstra(s, n, G);
    M = 0LL;
    for (int i = 0; i < n; ++i)
      if (D[i] < INF)
        M = max(M, D[i]);
    /*
    for(int i = 1; i < n; ++i){
      if(deg[i] && deg[p[i]]){
         G[i][p[i]] = d[i];
         G[p[i]][i] = d[i];
      }
    }
    for(int k = 0; k < n; ++k)
      for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
          G[i][j] = min(G[i][j],G[i][k] + G[k][j]);

    for(int i = 0; i < n; ++i)
      for(int j = i; j < n; ++j)
        if(G[i][j] < INF)
          M = max(M,G[i][j]);
    */
    ans -= M;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    long long int ans = 0LL;
    long long int G[n][n], inf = 10000000000;
    for (int i = 0; i < n; ++i)
      fill(G[i], G[i] + n, inf);
    for (int i = 0; i < n; ++i)
      G[i][i] = 0;
    int p[n], deg[n];
    fill(deg, deg + n, -1);
    for (int i = 1; i < n; ++i) {
      cin >> p[i];
      --p[i];
      ++deg[p[i]];
      ++deg[i];
    }
    int d[n];
    for (int i = 1; i < n; ++i) {
      cin >> d[i];
      if (deg[i] && deg[p[i]])
        ans += 3 * d[i];
      else
        ans += d[i];
    }
    for (int i = 1; i < n; ++i) {
      if (deg[i] && deg[p[i]]) {
        G[i][p[i]] = d[i];
        G[p[i]][i] = d[i];
      }
    }
    for (int k = 0; k < n; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    long long M = 0;
    for (int i = 0; i < n; ++i)
      for (int j = i; j < n; ++j)
        if (G[i][j] < inf)
          M = max(M, G[i][j]);
    ans -= M;
    cout << ans << endl;
  }
  return 0;
}