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. Python 购物车---之商家部分

    知识点:文件写入操作, 函数, 函数递归 #!C:\Program Files\Python35/bin # -*- conding:utf-8 -*- # author: Frank # 定义商品列 ...

  2. keil or c51 汇编调用c语言函数 容易忽视的问题

    最近,在用keil 写一个小程序时,想实践一下从汇编调用 C语言函数,我们都知道C语言调用汇编函数讨论得较多,但反过来,从汇编中调用C语言的函数未见深入分析:在开始的时候,还是忽视了一个问题,就是对现 ...

  3. Hdu5126-stars(两次CDQ分治)

    题意: 简化就是有两种操作,一种是插入(x,y,z)这个坐标,第二种是查询(x1,y1,z1)到(x2,y2,z2)(x1<=x2,y1<=y2,z1<=z2)的长方体包含多少个点. ...

  4. windows多线程同步总结

    1.多线程同步与多线程互斥的关系 其实这也是我一直困扰的问题,在这里我只是说说我的理解.我的理解是多线程互斥是针对于多线程资源而言的. 而多线程同步是针对于多线程时序问题.由于线程的并发性导致其运行时 ...

  5. devStack for Openstack dev Env

    devstack是一套用来给开发人员快速部署Openstack开发环境的脚本,其实对于整个安装过程没有什么好说的,因为脚本写的很完善,全程无脑式安装也没什么大问题,但是因为公司里的网络环境不给力,我的 ...

  6. extjs两个tbar问题

      版本:extjs3.4   接触过extjs的同志们都知道每个panel都有一个tbar(top bar 上面工具栏) ,bbar(bottom bar 底部工具栏)      大家做查询页面,一 ...

  7. 主题简介 ASP .NET

    由控件的外观.样式组成的集合,由一个文件组构成,存放在App_Themes文件夹下. 主题包括:皮肤文件(.Skin).CSS文件(.CSS).图片.其它资源等. 主题的作用:统一设置Web页面的外观 ...

  8. Linux安装中文man手冊

    1.下载中文包: http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.1.tar.gz/13275fd039 ...

  9. sql语句中查询出的数据添加一列,并且添加默认值

    查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...

  10. [跟我学spring学习笔记][IoC]

    IoC基础 什么是IoC Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. ioc做什么 IoC容器帮对象找相应的依赖对象并注入,而不是由对象主动去找 ...