Problem p03602 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#define FINAL_OUT(x)                                                           \
  {                                                                            \
    cout << (x) << '\n';                                                       \
    exit(0);                                                                   \
  }

int const maxn = 305;
int par[maxn];

inline int get_par(const int x) {
  return par[x] == x ? x : par[x] = get_par(par[x]);
}

inline void uni(const int x, const int y) { par[get_par(x)] = get_par(y); }

int main() {
  //    freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  for (auto &vec : a)
    for (int &x : vec)
      cin >> x;

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (a[i][k] + a[k][j] < a[i][j])
          FINAL_OUT(-1);

  vector<vector<int>> g(n, vector<int>(n, 1000000009));
  for (int i = 0; i < n; ++i)
    g[i][i] = 0;
  long long ans = 0;

  vector<tuple<int, int, int>> edges;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      edges.emplace_back(a[i][j], i, j);
  sort(begin(edges), end(edges));

  int cnt = n;
  for (int i = 0; i < n; ++i)
    par[i] = i;

  for (int i = 0; cnt > 1; ++i) {
    int w, x, y;
    tie(w, x, y) = edges[i];
    if (get_par(x) != get_par(y)) {
      g[x][y] = g[y][x] = w;
      uni(x, y);
      --cnt;
      ans += w;
    }
  }

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (g[i][k] + g[k][j] < g[i][j])
          g[i][j] = g[i][k] + g[k][j];

  for (const auto &e : edges) {
    int w, x, y;
    tie(w, x, y) = e;
    if (g[x][y] != w) {
      g[x][y] = g[y][x] = w;
      ans += w;
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            if (g[i][k] + g[k][j] < g[i][j])
              g[i][j] = g[i][k] + g[k][j];
    }
  }
  cout << ans << endl;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#define FINAL_OUT(x)                                                           \
  {                                                                            \
    cout << (x) << '\n';                                                       \
    exit(0);                                                                   \
  }

int const maxn = 305;
int par[maxn];

inline int get_par(const int x) {
  return par[x] == x ? x : par[x] = get_par(par[x]);
}

inline void uni(const int x, const int y) { par[get_par(x)] = get_par(y); }

int main() {
  //    freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  for (auto &vec : a)
    for (int &x : vec)
      cin >> x;

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (a[i][k] + a[k][j] < a[i][j])
          FINAL_OUT(-1);

  vector<vector<int>> g(n, vector<int>(n, 1000000009));
  for (int i = 0; i < n; ++i)
    g[i][i] = 0;
  long long ans = 0;

  vector<tuple<int, int, int>> edges;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      edges.emplace_back(a[i][j], i, j);
  sort(begin(edges), end(edges));

  int cnt = n;
  for (int i = 0; i < n; ++i)
    par[i] = i;

  for (int i = 0; cnt > 1; ++i) {
    int w, x, y;
    tie(w, x, y) = edges[i];
    if (get_par(x) != get_par(y)) {
      g[x][y] = g[y][x] = w;
      uni(x, y);
      --cnt;
      ans += w;
    }
  }

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (g[i][k] + g[k][j] < g[i][j])
          g[i][j] = g[i][k] + g[k][j];

  for (const auto &e : edges) {
    int w, x, y;
    tie(w, x, y) = e;
    if (g[x][y] != w) {
      g[x][y] = g[y][x] = w;
      ans += w;
      vector<int> r = {x, y};
      for (int k : r)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            if (g[i][k] + g[k][j] < g[i][j])
              g[i][j] = g[i][k] + g[k][j];
    }
  }
  cout << ans << endl;
}