CT25C - Roads in Berland

no tags 

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 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 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 distances. j-th integer in the i-th row — di, j, the 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 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, bin, aibi, 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 lines, each line contains one integers qi (1 ≤ ik). 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.

Example

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

Output:
17
12

hide comments
smso: 2024-11-02 15:26:33

Beware that the queries are cumulative!

Augusto: 2016-03-20 15:28:39

I got confused with the statement when it says "Also for each pair of cities it is known the distance between them". I don't know what it means with "the distance", I think it means "some distance" or something but it is NOT the shortest distance.

Last edit: 2016-03-20 15:35:23
Bhavik: 2014-02-23 15:00:39

nice question...:))WFI algo works here

Last edit: 2014-02-23 15:05:10
ymGXX: 2010-12-10 06:51:07

SPFA AC in 2.14s with constant-optimization

pankaj: 2010-10-22 08:09:20

sometime strange things happens. My AC solution gave TLE on second time submission. !!!

Last edit: 2010-10-22 08:09:31
LeppyR64: 2010-08-07 21:33:38

After each of the k roads is built does it continue to exist for the rest of the k roads?

3
0 4 8
4 0 4
8 4 0
2
1 2 1
2 3 1

Is the answer for this testcase:
10
10

Or:
10
4

Edit: The second answer is true(10 , 4)

Last edit: 2010-08-08 09:13:47
Mohamed Maher: 2010-08-07 13:11:00

is there any explaination for the test cases

Krunal Manik: 2010-08-07 09:40:46

@Phan Công Minh
The prob statement says :
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.
Edit: thanks.

Change that if your judge data is according to what you have said.

Last edit: 2010-08-07 09:38:07
Phan Công Minh: 2010-08-07 09:40:46

Acc on codeforces doesn't mean you'll get acc here. There's some different conditions between codeforces and Spoj.
Here ,the first matrix describes distance between a pair of city,but not the shortest distance .

Mohamed Maher: 2010-08-07 09:40:46

i think there is somthing wrong in test data i'm sure becz
me and islam have problem with submitting and true codes


Added by:Phan Công Minh
Date:2010-08-06
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 VB.NET
Resource:Based on codeforces