CF图论一--Roads in Berland--Floyed松弛

C. Roads in Berland

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

2
0 5
5 0
1
1 2 3

output

Copy

3 

input

Copy

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

output

Copy

17 12 

问给出新的边权值之后,各个点之间最短路径之和是多少。

直接三重循环不断求最短路是不行的,想到这个算法求最短路时第一重循环k枚举的是断点,于是直接把u、v当做断点就可以了,优化成二重循环。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=300+66;
const ll mod=1e9+7;
int N,M,Q;
int a[maxn][maxn];
int main()
{
    scanf("%d",&N);
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    scanf("%d",&Q);
    int u,v,w;
    while(Q--)
    {
        scanf("%d %d %d",&u,&v,&w);
        if(a[u][v]>w)
        {
            a[u][v]=min(w,a[u][v]);
            a[v][u]=min(w,a[u][v]);
            for(int j=1; j<=N; j++)
            {
                for(int i=1; i<=N; i++)
                {
                    a[i][j]=a[j][i]=min(a[j][i],a[j][u]+a[u][i]);
                }
            }
            for(int j=1; j<=N; j++)
            {
                for(int i=1; i<=N; i++)
                {
                    a[i][j]=a[j][i]=min(a[j][i],a[j][v]+a[v][i]);
                }
            }
        }
        ll s=0;
        for(int i=1; i<=N; i++)
        {
            for(int j=i+1; j<=N; j++)
            {
                s+=a[i][j];
            }
        }
        printf("%lld ",s);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值