CF25C.Roads in Berland

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn cities numbered from 1 to nn 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 kk 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.

输入格式

The first line contains integer nn ( 2<=n<=3002<=n<=300 ) — amount of cities in Berland. Then there follow nn lines with nn integer numbers each — the matrix of shortest distances. jj -th integer in the ii -th row — di,jd_{i,j} , the shortest distance between cities ii and jj . It is guaranteed that di,i=0,di,j=dj,id_{i,i}=0,d_{i,j}=d_{j,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 kk ( 1<=k<=3001<=k<=300 ) — amount of planned roads. Following kk lines contain the description of the planned roads. Each road is described by three space-separated integers aia_{i} , bib_{i} , cic_{i} ( 1<=ai,bi<=n,aibi,1<=ci<=10001<=a_{i},b_{i}<=n,a_{i}≠b_{i},1<=c_{i}<=1000 ) — aia_{i} and bib_{i} — pair of cities, which the road connects, cic_{i} — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.

输出格式

Output kk space-separated integers qiq_{i} ( 1<=i<=k1<=i<=k ). qiq_{i} should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to ii . 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.

输入输出样例

  • 输入#1

    2
    0 5
    5 0
    1
    1 2 3
    

    输出#1

    3 
  • 输入#2

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

    输出#2

    17 12 
首页