Description

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.

Sample Input

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 

题目的大概意思是有n个城市,现给出这n个城市之间没两个城市的距离,改变一些城市的距离,问最后所有这些路径长度最小之和。
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
long long n,m,a[310][310],sum,t1,t2,s;
while (cin>>n)
{
sum=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
cin>>a[i][j];
sum+=a[i][j];
}
sum/=2; //没条路算了两次,多以要除以2。
cin>>m;
for (int p=1;p<=m;p++)
{
cin>>t1>>t2>>s;
if (a[t1][t2]<s)
{
cout <<sum<<endl;
continue;
}
for (int i=1;i<=n;i++) //检查一下改变一条路之后对其它路有没有影响
for (int j=1;j<=n;j++)
{
long long temp=a[i][t1]+s+a[t2][j];
if (temp<a[i][j]) //如果改变t1到t2的距离,看看i到t1再到t2再到i的距离是否比i直接到j的距离短,是的话则改变i到j的距离,同时改变j到i的距离。
{
sum=sum-(a[i][j]-temp);
a[i][j]=temp;
a[j][i]=temp;
}
}
cout <<sum<<endl;
}
}
return 0;
}

  

Roads in Berland(图论)的更多相关文章

  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. Day4 - M - Roads in Berland CodeForces - 25C

    There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...

  3. 【Codeforces 25C】Roads in Berland

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

  4. C. Roads in Berland

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

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

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

  6. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

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

  9. codeforces 659E E. New Reform(图论)

    题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. WordPress插件开发记录

    1.a标签在新的网页中打开内容     <a href="网址" target="_blank"></a>      2.PDO的$re ...

  2. 对163k地方门户网站系统QQ互联功能修改

    163k地方门户网站QQ互联申请时遇到的问题: "禁止开发商强制用户重新注册或绑定其他帐号" 原因是用户登录完QQ还需要注册帐号或者绑定原有帐号 163k地方门户网站的QQ互联登录 ...

  3. C语言编程时常犯十八个错误

    C语言的最大特点是:功能强.使用方便灵活.C编译的程序对语法检查并不象其它高级语言那么严格,这就给编程人员留下“灵活的余地”,但还是由于这个灵活给程序的调试带来了许多不便,尤其对初学C语言的人来说,经 ...

  4. MySQL表复制

    http://www.2cto.com/database/201202/120259.html http://www.cnblogs.com/sunss/archive/2010/10/08/1845 ...

  5. 转:SVN 出现This client is too old to work with working copy...错误

    本地进行SVN客户端版本更新,但是之前一些代码是用的旧svn客户端提交的,这时候进行代码更新或者提交代码可能会出现错误,我这边是NetBeans中提交代码就出现了以下错误:This client is ...

  6. c++ 12

    一.模板与继承 1.从模板类派生模板子类 2.为模板子类提供基类 二.容器和迭代器 以链表为例. 三.STL概览 1.十大容器 1)向量(vector):连续内存,后端压弹,插删低效 2)列表(lis ...

  7. uva10245-The Closest Pair Problem(平面上的点分治)

    解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d]( ...

  8. 【转】关于android应用程序的入口

    android应用程序,由一到多个Activity组成.每个Activity没有很紧密的联系,因为我们可以在自己的程序中调用其它Activity,特别是调用自己的代码之外生成的Activity,比如a ...

  9. sem_timedwait的用法

    #include <semaphore.h> int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); Link ...

  10. pyqt一个简单的动画

    import sys from PyQt4.QtGui import QApplication , QGraphicsEllipseItem , QGraphicsItemAnimationfrom ...