Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef int64_t ll;
typedef long double ld;
const ll INF = (1LL << 60);
const ld pi = acosl((ld)1);
const ll mod = 1000000007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};

void solve() {
  ll n;
  cin >> n;
  ll A[n][n];
  rep(i, n) rep(j, n) cin >> A[i][j];

  ll d[n][n];
  rep(i, n) rep(j, n) d[i][j] = INF;
  rep(i, n) d[i][i] = 0;
  ll ans = 0;
  set<pair<ll, ll>> set;
  rep(i, n) {
    // 最小
    ll num = INF;
    rep(j, n) {
      if (i == j)
        continue;
      num = min(num, A[i][j]);
    }

    rep(j, n) {
      if (num == A[i][j]) {
        // 辺を追加
        d[i][j] = A[i][j];
        d[j][i] = A[j][i];
        if (set.find(make_pair(i, j)) == set.end())
          ans += A[i][j];
        set.insert(make_pair(i, j));
        set.insert(make_pair(j, i));
      }
    }
  }

  // ワーシャルフロイド
  rep(k, n) rep(i, n) rep(j, n) { d[i][j] = min(d[i][k] + d[k][j], d[i][j]); }

  /*rep(i,n){
    rep(j,n)cout<<d[i][j]<<" ";
    cout<<endl;
  }*/

  vector<pair<ll, pair<ll, ll>>> v;
  rep(i, n) rep(j, n) {
    if (d[i][j] < A[i][j]) {
      // アウト
      cout << -1 << endl;
      return;
    }

    // 小さいなら変を追加する?
    if (d[i][j] > A[i][j]) {
      v.push_back(make_pair(A[i][j], make_pair(i, j)));
    }
  }

  sort(all(v));

  for (auto &&p : v) {
    ll a = p.second.first;
    ll b = p.second.second;
    if (d[a][b] == A[a][b])
      continue;
    ans += A[a][b];
    d[a][b] = A[a][b];
    d[b][a] = A[b][a];

    // わーシャルフロイド
    rep(k, n) rep(i, n) rep(j, n) { d[i][j] = min(d[i][k] + d[k][j], d[i][j]); }
    bool isOK = true;
    rep(i, n) rep(j, n) {
      if (d[i][j] < A[i][j]) {
        // アウト
        cout << -1 << endl;
        return;
      }
      if (d[i][j] != A[i][j])
        isOK = false;
    }
    if (isOK)
      break;
  }

  // ワーシャルフロイド
  rep(k, n) rep(i, n) rep(j, n) { d[i][j] = min(d[i][k] + d[k][j], d[i][j]); }
  rep(i, n) rep(j, n) {
    if (d[i][j] < A[i][j]) {
      // アウト
      cout << -1 << endl;
      return;
    }
  }

  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout << setprecision(30);
  solve();
}


Fixed submission:
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef int64_t ll;
typedef long double ld;
const ll INF = (1LL << 60);
const ld pi = acosl((ld)1);
const ll mod = 1000000007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};

void solve() {
  ll n;
  cin >> n;
  ll A[n][n];
  rep(i, n) rep(j, n) cin >> A[i][j];

  ll d[n][n];
  rep(i, n) rep(j, n) d[i][j] = INF;
  rep(i, n) d[i][i] = 0;
  ll ans = 0;
  set<pair<ll, ll>> set;
  rep(i, n) {
    // 最小
    ll num = INF;
    rep(j, n) {
      if (i == j)
        continue;
      num = min(num, A[i][j]);
    }

    rep(j, n) {
      if (num == A[i][j]) {
        // 辺を追加
        d[i][j] = A[i][j];
        d[j][i] = A[j][i];
        if (set.find(make_pair(i, j)) == set.end())
          ans += A[i][j];
        set.insert(make_pair(i, j));
        set.insert(make_pair(j, i));
      }
    }
  }

  // ワーシャルフロイド
  rep(k, n) rep(i, n) rep(j, n) { d[i][j] = min(d[i][k] + d[k][j], d[i][j]); }

  /*rep(i,n){
    rep(j,n)cout<<d[i][j]<<" ";
    cout<<endl;
  }*/

  vector<pair<ll, pair<ll, ll>>> v;
  rep(i, n) rep(j, n) {
    if (d[i][j] < A[i][j]) {
      // アウト
      cout << -1 << endl;
      return;
    }

    // 小さいなら変を追加する?
    if (d[i][j] > A[i][j]) {
      v.push_back(make_pair(A[i][j], make_pair(i, j)));
    }
  }

  sort(all(v));

  for (auto &&p : v) {
    ll a = p.second.first;
    ll b = p.second.second;
    if (d[a][b] == A[a][b])
      continue;
    ans += A[a][b];
    d[a][b] = A[a][b];
    d[b][a] = A[b][a];

    // わーシャルフロイド
    rep(i, n) rep(j, n) { d[i][j] = min(d[i][a] + d[b][j] + d[a][b], d[i][j]); }
    rep(i, n) rep(j, n) { d[i][j] = min(d[i][b] + d[a][j] + d[a][b], d[i][j]); }
    bool isOK = true;
    rep(i, n) rep(j, n) {
      if (d[i][j] < A[i][j]) {
        // アウト
        cout << -1 << endl;
        return;
      }
      if (d[i][j] != A[i][j])
        isOK = false;
    }
    if (isOK)
      break;
  }

  // ワーシャルフロイド
  rep(k, n) rep(i, n) rep(j, n) { d[i][j] = min(d[i][k] + d[k][j], d[i][j]); }
  rep(i, n) rep(j, n) {
    if (d[i][j] < A[i][j]) {
      // アウト
      cout << -1 << endl;
      return;
    }
  }

  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout << setprecision(30);
  solve();
}