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 aibici (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.

Examples

Input
2
0 5
5 0
1
1 2 3
Output
3 
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12 

思路:要求每一对的最短路,直接想到floyd,但是每次都全跑一次一定会超时,那就每次把修改的边作为中间点进行floyd即可,代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int maxm = ; int G[maxm][maxm];
int N, K; void calculate() {
for(int k = ; k <= N; ++k)
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j)
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
} int main() {
scanf("%d", &N);
int t;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
scanf("%d", &G[i][j]);
}
calculate();
scanf("%d", &K);
int u, v;
for(int f = ; f < K; ++f) {
scanf("%d%d%d", &u, &v, &t);
LL ans = ;
if(t < G[u][v]) {
G[u][v] = G[v][u] = t;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
G[i][j] = min(G[i][j], min(G[i][u]+G[u][j], G[i][v]+G[v][j]));
G[j][i] = G[i][j];
}
}
for(int i = ; i < N; ++i)
for(int j = i+; j <= N; ++j)
ans += G[i][j];
printf("%I64d ", ans);
}
return ;
}

Day4 - M - Roads in Berland CodeForces - 25C的更多相关文章

  1. Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

    C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. CodeForces 25C(Floyed 最短路)

    F - Roads in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  3. 【Codeforces 25C】Roads in Berland

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...

  4. Roads in Berland(图论)

    Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...

  5. C. Roads in Berland

    题目链接: http://codeforces.com/problemset/problem/25/C 题意: 给一个最初的所有点与点之间的最短距离的矩阵.然后向图里加边,原有的边不变,问加边后的各个 ...

  6. Chemistry in Berland CodeForces - 846E

    题目 题意: 有n种化学物质,第i种物质现有bi千克,需要ai千克.有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克 ...

  7. Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

    E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)

    Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...

随机推荐

  1. MyBatis+Oracle实现主键自增长的几种常用方式

    一.使用selectKey标签 <insert id="addLoginLog" parameterType="map" > <selectK ...

  2. 让 el-dialog 居中,并且内容多的时候内部可以滚动

    .el-dialog { position: absolute; top: 50%; left: 50%; margin: 0 !important; transform: translate(-50 ...

  3. Oracle Parallel使用方法

    一. 并行查询 并行查询允许将一个sql select语句划分为多个较小的查询,每个部分的查询并发地运行,然后将各个部分的结果组合起来,提供最终的结果,多用于全表扫描,索引全扫描等,大表的扫描和连接. ...

  4. NGINX生命周期-转

  5. python爬虫(三) 用request爬取拉勾网职位信息

    request.Request类 如果想要在请求的时候添加一个请求头(增加请求头的原因是,如果不加请求头,那么在我们爬取得时候,可能会被限制),那么就必须使用request.Request类来实现,比 ...

  6. Java - 使用hibernate配置文件 + JPA annotation注解操作数据库

    本程序运行环境:IDEA. 实际上我对hiberbate与注解的关系还不是太清晰.据我所知注解都是Java JPA的,那么我的理解是:hibernate就应该只是通过这些JPA标识及hibernate ...

  7. JS拖拽-面向对象拖拽-继承

    //普通拖拽 /* * CSS */ #div1{ width:100px; height:100px; position:absolute; background:red; cursor:move; ...

  8. Django学习 之 Django安装与一个简单的实例认识

    一.Django简介 1.MVC与MTV模型 (1)MVC模型 Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的. ...

  9. MySQL 之存储引擎与数据类型与数据约束

    一.存储引擎场景 1.InnoDB 用于事务处理应用程序,支持外键和行级锁.如果应用对事物的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包括很多更新和删除操作,那 ...

  10. C++ STL vector容量(capacity)和大小(size)的区别

    很多初学者分不清楚 vector 容器的容量(capacity)和大小(size)之间的区别,甚至有人认为它们表达的是一个意思.本节将对 vector 容量和大小各自的含义做一个详细的介绍. vect ...