Roads in Berland (Codeforces 25C)
Tags: Others, shortest paths

There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them — for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.

Input: The first line contains integer n (2 ≤ n ≤ 300) — amount of cities in Berland. Then there follow n lines with n integer numbers each — the matrix of shortest distances. j-th integer in the i-th row — di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.Next line contains integer k (1 ≤ k ≤ 300) — amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.

Output: Output k space-separated integers qi (1 ≤ i ≤ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.


Sample Inputs:
2
0 5
5 0
1
1 2 3

3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1

Sample Outputs:
3

17 12

Solution (Python):
n = int(input())
matrix = [[] for _ in range(n)]
for i in range(n):
    matrix[i] = list(map(int, input().split()))
s = 0
for i in range(n):
    for j in range(i + 1, n):
        s += matrix[i][j]
k = int(input())
sums = []
for _ in range(k):
    (a, b, c) = map(int, input().split())
    a -= 1
    b -= 1
    for i in range(n):
        for j in range(i + 1, n):
            aux = min(matrix[i][a] + c + matrix[b][j], matrix[i][b] + c + matrix[a][j])
            if matrix[i][j] > aux:
                s += aux - matrix[i][j]
                matrix[i][j] = matrix[j][i] = aux
    sums.append(s)
print(' '.join(map(str, sums)))

Solution (Python):
n = int(input())
matrix = [[] for _ in range(n)]
for i in range(n):
    matrix[i] = list(map(int, input().split()))
s = 0
for i in range(n):
    for j in range(i + 1, n):
        s += matrix[i][j]
k = int(input())
sums = []
for _ in range(k):
    (a, b, c) = map(int, input().split())
    a -= 1
    b -= 1
    for i in range(n):
        for j in range(i + 1, n):
            aux = min(matrix[i][a] + c + matrix[b][j], matrix[i][b] + c + matrix[a][j])
            if matrix[i][j] > aux:
                s += aux - matrix[i][j]
                matrix[i][j] = matrix[j][i] = aux
    sums.append(s)
print(' '.join(map(str, sums)))

Solution (Python):
import io, os, sys
ppyin = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
cpyin = sys.stdin.readline
input = ppyin if 'PyPy' in sys.version else cpyin

def main():
    n = int(input())
    dis = [[] for x in range(n)]
    for i in range(n):
        dis[i] = [int(x) for x in input().split()]
    k = int(input())
    res = []
    for q in range(k):
        (a, b, c) = [int(x) for x in input().split()]
        a -= 1
        b -= 1
        dis[a][b] = dis[b][a] = min(dis[a][b], c)
        for i in range(n):
            dis[a][i] = min(dis[a][i], dis[a][b] + dis[b][i])
            dis[b][i] = min(dis[b][i], dis[b][a] + dis[a][i])
        for i in range(n):
            for j in range(i + 1, n):
                pa = dis[i][a] + dis[a][b] + dis[b][j]
                pb = dis[i][b] + dis[b][a] + dis[a][j]
                dis[i][j] = dis[j][i] = min(dis[i][j], min(pa, pb))
        tot = 0
        for i in range(n):
            for j in range(i + 1, n):
                tot += dis[i][j]
        res.append(tot)
    print(*res)
if __name__ == '__main__':
    main()