Codeforces Beta Round #25 (Div. 2 Only)

来源:互联网 发布:软件平台下载 编辑:程序博客网 时间:2024/06/05 18:04

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
20 55 011 2 3
output
3 
input
30 4 54 0 95 9 022 3 81 2 1
output
17 12 


题意:

已经给你最短路的邻接矩阵,然后每次给你一条路,连续求出每个点之间的最短路总和。


POINT:

即判断每次能不能进行松弛,因为给你一条路,只能松弛有关这两个点的路。


#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define LL long longconst LL maxn = 300+5;LL mp[maxn][maxn];LL ans=0;LL u,v,w;LL n;void doit(){for(LL i=1;i<=n;i++){for(LL j=1;j<=n;j++){if(mp[i][j]>mp[i][u]+mp[u][j]){ans-=mp[i][j]-(mp[i][u]+mp[u][j]);mp[i][j]=mp[j][i]=mp[i][u]+mp[u][j];}if(mp[i][j]>mp[i][v]+mp[v][j]){ans-=mp[i][j]-(mp[i][v]+mp[v][j]);mp[i][j]=mp[j][i]=mp[i][v]+mp[v][j];}}}}int main(){scanf("%lld",&n);for(LL i=1;i<=n;i++){for(LL j=1;j<=n;j++){scanf("%lld",&mp[i][j]);if(i<j) ans+=mp[i][j];}}LL m;scanf("%lld",&m);for(LL i=1;i<=m;i++){scanf("%lld %lld %lld",&u,&v,&w);if(mp[u][v]<=w){printf("%lld ",ans);continue;}else{ans-=mp[u][v]-w;mp[u][v]=mp[v][u]=w;doit();printf("%lld ",ans);}}}



原创粉丝点击