Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

// intやllをstringに変換
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> G[i][j];
    }
  }
  vector<vector<int>> G2;
  G2 = G;
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        chmin(G2[i][j], G2[i][k] + G2[k][j]);
      }
    }
  }
  bool changed = false;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (G2[i][j] != G[i][j]) {
        changed = true;
      }
    }
  }
  if (changed) {
    cout << -1 << endl;
  } else {
    vector<vector<P>> G3(N);
    priority_queue<pair<int, P>, vector<pair<int, P>>, greater<>> Q;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        Q.push(make_pair(G[i][j], make_pair(i, j)));
      }
    }
    ll ans = 0;
    while (Q.size()) {
      auto edge = Q.top();
      Q.pop();
      priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> Q2;
      Q2.push(make_pair(0, edge.second.first));
      vector<ll> dist(N, INF);
      dist[edge.second.first] = 0;
      while (Q2.size()) {
        auto p = Q2.top();
        Q2.pop();
        if (dist[p.second] < p.first)
          continue;
        for (auto nv : G3[p.second]) {
          if (dist[nv.first] <= p.first + nv.second)
            continue;
          dist[nv.first] = p.first + nv.second;
          Q2.push(make_pair(dist[nv.first], nv.first));
        }
      }
      int num = dist[edge.second.second];
      if (edge.first != num) {
        ans += edge.first;
        G3[edge.second.first].push_back(
            make_pair(edge.second.second, edge.first));
        G3[edge.second.second].push_back(
            make_pair(edge.second.first, edge.first));
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

// intやllをstringに変換
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> G[i][j];
    }
  }
  vector<vector<int>> G2;
  G2 = G;
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        chmin(G2[i][j], G2[i][k] + G2[k][j]);
      }
    }
  }
  bool changed = false;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (G2[i][j] != G[i][j]) {
        changed = true;
      }
    }
  }
  if (changed) {
    cout << -1 << endl;
  } else {
    vector<vector<P>> G3(N);
    priority_queue<pair<int, P>, vector<pair<int, P>>, greater<>> Q;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (i == j)
          continue;
        Q.push(make_pair(G[i][j], make_pair(i, j)));
      }
    }
    ll ans = 0;
    while (Q.size()) {
      auto edge = Q.top();
      Q.pop();
      priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> Q2;
      Q2.push(make_pair(0, edge.second.first));
      vector<ll> dist(N, INF);
      dist[edge.second.first] = 0;
      while (Q2.size()) {
        auto p = Q2.top();
        Q2.pop();
        if (dist[p.second] < p.first)
          continue;
        for (auto nv : G3[p.second]) {
          if (dist[nv.first] <= p.first + nv.second)
            continue;
          dist[nv.first] = p.first + nv.second;
          Q2.push(make_pair(dist[nv.first], nv.first));
        }
      }
      int num = dist[edge.second.second];
      if (edge.first != num) {
        ans += edge.first;
        G3[edge.second.first].push_back(
            make_pair(edge.second.second, edge.first));
        G3[edge.second.second].push_back(
            make_pair(edge.second.first, edge.first));
      }
    }
    cout << ans << endl;
  }
  return 0;
}