Problem:
 Score : 500 points    Problem Statement In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.
The following are known about the road network:   People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.  Different roads may have had different lengths, but all the lengths were positive integers.   Snuke the archeologist found a table with N rows and N columns, A , in the ruin of Takahashi Kingdom.
He thought that it represented the shortest distances between the cities along the roads in the kingdom.  Determine whether there exists a road network such that for each u and v , the integer A_{u, v} at the u -th row and v -th column of A is equal to the length of the shortest path from City u to City v .
If such a network exist, find the shortest possible total length of the roads.      Constraints  1 \leq N \leq 300  If i ≠ j , 1 \leq A_{i, j} = A_{j, i} \leq 10^9 .  A_{i, i} = 0         Inputs Input is given from Standard Input in the following format:  N  A_{1, 1}  A_{1, 2}  ...  A_{1, N}  A_{2, 1}  A_{2, 2}  ...  A_{2, N}  ...  A_{N, 1}  A_{N, 2}  ...  A_{N, N}       Outputs If there exists no network that satisfies the condition, print -1 .
If it exists, print the shortest possible total length of the roads.        Sample Input 1 3
0 1 3
1 0 2
3 2 0      Sample Output 1 3  The network below satisfies the condition:   City 1 and City 2 is connected by a road of length 1 .  City 2 and City 3 is connected by a road of length 2 .  City 3 and City 1 is not connected by a road.        Sample Input 2 3
0 1 3
1 0 1
3 1 0      Sample Output 2 -1  As there is a path of length 1 from City 1 to City 2 and City 2 to City 3 , there is a path of length 2 from City 1 to City 3 .
However, according to the table, the shortest distance between City 1 and City 3 must be 3 .  Thus, we conclude that there exists no network that satisfies the condition.       Sample Input 3 5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0      Sample Output 3 82       Sample Input 4 3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0      Sample Output 4 3000000000

Solution (Python):
def solve(n, tbl):
    ans = 0
    for i, row_i in enumerate(tbl):
        for j in range(i + 1, n):
            row_j = tbl[j]
            ij = row_i[j]
            mind = min((ik + kj for ik, kj in zip(row_i, row_j) if ik and kj), default=float('inf'))
            if ij > mind:
                return -1
            if ij < mind:
                ans += ij
    return ans


n = int(input())
tbl = [list(map(int, input().split())) for _ in range(n)]
print(solve(n, tbl))

Solution (Python):
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()

Solution (Python):
import numpy as np
import scipy.sparse.csgraph as csg

if __name__ == '__main__':
    N = int(input())
    A = [list(map(int, input().split())) for i in range(N)]

    G = np.asarray(A, dtype=np.uint32)
    G2 = csg.floyd_warshall(G, directed=False)
    
    
    
    if np.any(G != G2):
        print(-1)
        exit()
    ans = 0
    G[G == 0] = 1
    for i in range(N):
        for j in range(i + 1, N):
            n = np.min(G[i] + G[j])
            if n > G[i, j]:
                ans += G[i, j]

    print(ans)

Solution (Python):
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

INF = 10 ** 9 + 1
for i in range(N):
    A[i][i] = INF

ans = 0
for i in range(N):
    Ai = A[i]
    for j in range(i):
        d = min(map(sum, zip(Ai, A[j])))

        if A[i][j] > d:
            print('-1')
            exit()
        elif A[i][j] < d:
            ans += A[i][j]

print(ans)

Solution (Python):
N = int(raw_input())
A = []
for n in xrange(N):
  A.append(map(int, raw_input().split(' ')))
  A[n][n] = 1000000001

answer = 0
for i in xrange(N):
  Ai = A[i]
  for j in xrange(i):
    Aij = Ai[j]
    L = min(map(sum, zip(Ai, A[j])))
    if Aij < L:
      answer += Aij
    else:
      if Aij > L:
        print -1
        exit()
print answer

Solution (Python):
N = int(raw_input())
A = []
for n in xrange(N):
  A.append(map(int, raw_input().split(' ')))
  A[n][n] = 1000000001

answer = 0
for i in range(N):
  Ai = A[i]
  for j in range(i):
    Aij = Ai[j]
    L = min(map(sum, zip(Ai, A[j])))
    if Aij < L:
      answer += Aij
    else:
      if Aij > L:
        print -1
        exit()
print answer

Solution (Python):

N = int(input())

import numpy as np

G = np.array([list(map(int,input().split())) for i in range(N)], dtype=np.uint32)

import scipy.sparse.csgraph as csg

G2 = csg.floyd_warshall(G,directed=False)

if np.any(G != G2):
  print(-1)
else:
  G[G==0] = 2*10**9
  t = 0
  for i in range(N):
    for j in range(i+1,N):
      n = np.min(G[i]+G[j])
      if n > G[i,j]:
        t += G[i,j]


  print(t)

Solution (Python):
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]

ans = 0; ok = 1
for i in range(n):
    for j in range(i+1, n):
        cost = A[i][j]
        for k in range(n):
            if i == k or j == k:
                continue
            if A[i][k] + A[k][j] < A[i][j]:
                ok = 0
            elif A[i][k] + A[k][j] == A[i][j]:
                cost = 0
        ans += cost
print(ans if ok else -1)

Solution (Python):
def main():
    n = int(input())
    a = [list(map(int, input().split())) for i in range(n)]

    total = sum([sum(a[i]) for i in range(n)])
    for k in range(n):
        for i in range(n):
            if k == i:
                continue
            for j in range(n):
                if i == j or k == j:
                    continue
                if a[i][j] > a[i][k] + a[k][j]:
                    print(-1)
                    exit()
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if i == k or j == k:
                    continue
                if a[i][j] == a[i][k] + a[k][j]:
                    total -= a[i][j]
                    break

    print(total//2)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

exist = True
ans = 0
ls = []
for i in range(n - 1):
    for j in range(i + 1, n):
        ls.append((a[i][j], i, j))
ls = list(reversed(sorted(ls)))
for (d, u, v) in ls:
    use = True
    for w in range(n):
        if u == w or v == w:
            continue
        if a[u][v] == a[u][w] + a[w][v]:
            use = False
        elif a[u][w] + a[w][v] < a[u][v]:
            exist = False
    if use:
        ans += d
if not exist:
    print(-1)
else:
    print(ans)

Solution (Python):

import numpy as np
N = int(input())
A = np.array([list(map(int, input().split())) for _ in range(N)], dtype=int)

res = A.sum() // 2

A_ = A.copy()


for i in range(N):
    A_[i, i] = 1000000001


A3 = np.zeros((N, N, N), dtype=int)
for i in range(N):
    A3[i, :, :] += A_
for j in range(N):
    A3[:, :, j] += A_
    
A3_min = A3.min(axis=1)

for i in range(N-1):
    for j in range(i+1, N):
        if A3_min[i, j] < A[i, j]:
            res = -1
        elif A3_min[i, j] == A[i, j]:
            res -= A[i, j]
        
        
if res < 0:
    print(-1)
else:
    print(res)

Solution (Python):
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
B = [e[:] for e in A]

for k in range(n):
    for i in range(n):
        for j in range(n):
            B[i][j] = min(B[i][k] + B[k][j], B[i][j])
ok = 1
for i in range(n):
    for j in range(n):
        if A[i][j] != B[i][j]:
            ok = 0
if not ok:
    print(-1)
    exit(0)

ans = 0
for i in range(n):
    for j in range(i+1, n):
        need = 1
        for k in range(n):
            if i == k or j == k:
                continue
            if A[i][k] + A[k][j] == A[i][j]:
                need = 0
        if need:
            ans += A[i][j]
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class UnionFind:
    def __init__(self, size):
        self.table = [-1 for _ in range(size)]

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] <= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
            return True
        return False

    def subsetall(self):
        a = []
        for i in range(len(self.table)):
            if self.table[i] < 0:
                a.append((i, -self.table[i]))
        return a

def main():
    n = I()
    a = [LI() for _ in range(n)]
    for i in range(n):
        ai = a[i]
        for j in range(i+1,n):
            aij = ai[j]
            aj = a[j]
            for k in range(n):
                if ai[k] + aj[k] < aij:
                    return -1

    r = 0
    ij = sorted(list(itertools.chain.from_iterable([[[a[i][j], i, j] for j in range(i+1,n)] for i in range(n)])))
    uf = UnionFind(n)
    aa = [[inf] * n for _ in range(n)]
    for i in range(n):
        aa[i][i] = 0
    for k,i,j in ij:
        if uf.union(i,j):
            aa[i][j] = aa[j][i] = k
            r += k
        elif aa[i][j] > k:
            f = True
            for l in range(n):
                if f and aa[l][i] + aa[l][j] <= k:
                    aa[i][j] = aa[j][i] = aa[l][i] + aa[l][j]
                    f = False
                elif aa[l][i] + aa[l][j] < aa[i][j]:
                    aa[i][j] = aa[j][i] = aa[l][i] + aa[l][j]

            if f:
                aa[i][j] = aa[j][i] = k
                r += k

    for i in range(n):
        ai = a[i]
        aai = aa[i]
        for j in range(i+1,n):
            if ai[j] != aai[j]:
                return -1

    return r


print(main())