Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define LL long long

#define fi first
#define se second
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(a, b) (((a) + (b)) >> 1)
#define maxx(a, b) ((a) < (b) ? (b) : (a))
#define minx(a, b) ((a) < (b) ? (a) : (b))
#define absx(a) ((a) < 0 ? -(a) : (a))
#define mk(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define itr iterator
#define lowbit(x) ((x) & -(x))

typedef unsigned LL ULL;
typedef unsigned uint;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pLL;

template <typename T> inline void read(T &x) {
  bool f;
  char ch;
  f = 0;
  x = 0;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (f)
    x = -x;
}

template <typename T> inline void Max(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void Min(T &a, T b) {
  if (b < a)
    a = b;
}
template <typename T> inline T Abs(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}

const double pi = (double)acos(-1.0);
const int MOD = (int)1e9 + 7;
const int INF = (int)0x3f3f3f3f;
const LL LINF = (LL)INF << 32 | INF;
const int SINF = (uint)~0 >> 1;
const LL SLINF = (ULL)(-1) >> 1;
const double DINF = (double)1e50;
const double eps = (double)1e-8;
const int maxn = (int)4e3 + 20;
const int maxm = (int)1e5 + 20;
const int maxk = (int)5e2 + 20;

inline int sig(double x) { return x < -eps ? -1 : x > eps; }

//--------------start------------------

int n;
int a[105][105], b[105][105];

void work() {
  read(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      read(a[i][j]), b[i][j] = a[i][j];

  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        Min(a[i][j], a[i][k] + a[k][j]);

  bool flag = 0;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      flag |= a[i][j] != b[i][j];

  if (flag) {
    puts("-1");
    return;
  }

  LL ans = 0;

  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      bool flag = 1;
      for (int k = 0; k < n; k++)
        if (k != i && k != j && a[i][j] == a[i][k] + a[k][j])
          flag = 0;

      if (flag)
        ans += a[i][j];
    }

  cout << ans << endl;
}

//--------------end--------------------

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif // yukihana0416
  work();
  return 0;
}


Fixed submission:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define LL long long

#define fi first
#define se second
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(a, b) (((a) + (b)) >> 1)
#define maxx(a, b) ((a) < (b) ? (b) : (a))
#define minx(a, b) ((a) < (b) ? (a) : (b))
#define absx(a) ((a) < 0 ? -(a) : (a))
#define mk(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define itr iterator
#define lowbit(x) ((x) & -(x))

typedef unsigned LL ULL;
typedef unsigned uint;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pLL;

template <typename T> inline void read(T &x) {
  bool f;
  char ch;
  f = 0;
  x = 0;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (f)
    x = -x;
}

template <typename T> inline void Max(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void Min(T &a, T b) {
  if (b < a)
    a = b;
}
template <typename T> inline T Abs(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}

const double pi = (double)acos(-1.0);
const int MOD = (int)1e9 + 7;
const int INF = (int)0x3f3f3f3f;
const LL LINF = (LL)INF << 32 | INF;
const int SINF = (uint)~0 >> 1;
const LL SLINF = (ULL)(-1) >> 1;
const double DINF = (double)1e50;
const double eps = (double)1e-8;
const int maxn = (int)4e3 + 20;
const int maxm = (int)1e5 + 20;
const int maxk = (int)5e2 + 20;

inline int sig(double x) { return x < -eps ? -1 : x > eps; }

//--------------start------------------

int n;
int a[305][305], b[305][305];

void work() {
  read(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      read(a[i][j]), b[i][j] = a[i][j];

  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        Min(a[i][j], a[i][k] + a[k][j]);

  bool flag = 0;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      flag |= a[i][j] != b[i][j];

  if (flag) {
    puts("-1");
    return;
  }

  LL ans = 0;

  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      bool flag = 1;
      for (int k = 0; k < n; k++)
        if (k != i && k != j && a[i][j] == a[i][k] + a[k][j])
          flag = 0;

      if (flag)
        ans += a[i][j];
    }

  cout << ans << endl;
}

//--------------end--------------------

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif // yukihana0416
  work();
  return 0;
}