0
点赞
收藏
分享

微信扫一扫

CF-25C - Roads in Berland(水题)

绣文字 2023-02-24 阅读 97



C - Roads in Berland

Crawling in process...

Crawling failed

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u


​​Submit​​​ ​​​Status​​​ ​​​Practice​​​ ​​​CodeForces 25C​​


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



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 ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci



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



思路:每次更新新加的边所影响的所有城市的最小距离。

#include<iostream>
#include<cstring>
using namespace std;
const int mm=310;
int map[mm][mm];
int main()
{
int m,n;
while(cin>>m)
{
memset(map,0,sizeof(map));
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
cin>>map[i][j];
cin>>n;
int a,b,c;
long long sum;
for(int i=0;i<n;i++)
{
cin>>a>>b>>c;
sum=0;
a--;b--;
for(int j=0;j<m;j++)
for(int k=0;k<m;k++)
{
map[j][k]=min(min(map[j][k],map[j][a]+map[b][k]+c),map[j][b]+map[a][k]+c);
sum+=map[j][k];
}
cout<<sum/2<<" ";
}
cout<<"\n";
}
}





举报

相关推荐

0 条评论