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 in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)


/**
 * DA-IICT
 * Author : Savaliya Sagar
 */
import java.io.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;

public class Main
{
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	long mind = Long.MAX_VALUE;
	HashSet<Pair1> g[];
	int v[], d[];
	
	void solve()
	{
		int n = ni();
		ArrayList<Pair> a = new ArrayList<>();
		g = new HashSet[n + 1];
		v = new int[n + 1];
		d = new int[n + 1];
		for (int i = 0; i <= n; i++)
			g[i] = new HashSet<>();
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				long temp = ni();
				if (i > j)
				{
					a.add(new Pair(i + 1, j + 1, temp));
					g[i + 1].add(new Pair1(j + 1, temp));
					g[j + 1].add(new Pair1(i + 1, temp));
				}
			}
		}
		long d[][] = new long[n + 1][n + 1];
		for (int i = 1; i <= n; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				d[i][j] = dijkstra(i, n, j);
				d[j][i] = d[i][j];
			}
		}
		sort(a, Collections.reverseOrder());
		long total = 0;
		for (int i = 0; i < a.size(); i++)
		{
			Pair x = a.get(i);
			if (d[x.u][x.v] != x.w)
			{
				out.println(-1);
				return;
			}
			g[x.u].remove(new Pair1(x.v, x.w));
			g[x.v].remove(new Pair1(x.u, x.w));
			long dt = dijkstra(x.u, n, x.v);
			if (dt != x.w)
			{
				total += x.w;
				g[x.u].add(new Pair1(x.v, x.w));
				g[x.v].add(new Pair1(x.u, x.w));
			}
		}
		out.println(total);
	}
	
	public long dijkstra(int source, int n, int d)
	{
		long dist[] = new long[n + 1];
		boolean visited[] = new boolean[n + 1];
		Arrays.fill(dist, Long.MAX_VALUE);
		PriorityQueue<Pair1> pq = new PriorityQueue<>();
		dist[source] = 0;
		pq.add(new Pair1(dist[source], source));
		while (!pq.isEmpty())
		{
			Pair1 cur = pq.poll();
			int current = (int) cur.v;
			visited[current] = true;
			for (Pair1 p : g[current])
			{
				if (!visited[(int) p.u])
				{
					if (dist[(int) p.u] > dist[current] + p.v)
					{
						dist[(int) p.u] = dist[current] + (int) p.v;
						pq.add(new Pair1(dist[(int) p.u], p.u));
					}
				}
			}
		}
		return dist[d];
	}
	
	class Pair1 implements Comparable<Pair1>
	{
		long u;
		long v;
		
		public Pair1(long u, long v)
		{
			this.u = u;
			this.v = v;
		}
		
		public int hashCode()
		{
			int hu = (int) (u ^ (u >>> 32));
			int hv = (int) (v ^ (v >>> 32));
			return 31 * hu + hv;
		}
		
		public boolean equals(Object o)
		{
			Pair1 other = (Pair1) o;
			return u == other.u && v == other.v;
		}
		
		public int compareTo(Pair1 other)
		{
			return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
		}
		
		public String toString()
		{
			return "[u=" + u + ", v=" + v + "]";
		}
	}
	
	class Pair implements Comparable<Pair>
	{
		int u;
		int v;
		long w;
		
		public Pair(int u, int v, long w)
		{
			this.u = u;
			this.v = v;
			this.w = w;
		}
		
		public int hashCode()
		{
			return Objects.hash();
		}
		
		public boolean equals(Object o)
		{
			Pair other = (Pair) o;
			return u == other.u && v == other.v;
		}
		
		public int compareTo(Pair other)
		{
			return Long.compare(w, other.w);
		}
		
		public String toString()
		{
			return "[u=" + u + ", v=" + v + ", w=" + w + "]";
		}
	}
	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if (!INPUT.isEmpty())
			tr(System.currentTimeMillis() - s + "ms");
	}
	
	public static void main(String[] args) throws Exception
	{
		new Thread(null, new Runnable()
		{
			public void run()
			{
				try
				{
					new Main().run();
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}, "1", 1 << 26).start();
	}
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf)
		{
			ptrbuf = 0;
			try
			{
				lenbuf = is.read(inbuf);
			} catch (IOException e)
			{
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c)
	{
		return !(c >= 33 && c <= 126);
	}
	
	private int skip()
	{
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}
	
	private double nd()
	{
		return Double.parseDouble(ns());
	}
	
	private char nc()
	{
		return (char) skip();
	}
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b)))
		{ // when nextLine, (isSpaceChar(b) && b !=
			// ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b)))
		{
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-')
		{
			minus = true;
			b = readByte();
		}
		
		while (true)
		{
			if (b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			} else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-')
		{
			minus = true;
			b = readByte();
		}
		
		while (true)
		{
			if (b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			} else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private void tr(Object... o)
	{
		System.out.println(Arrays.deepToString(o));
	}
}