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 thatdi, 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 — 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
2 0 5 5 0 1 1 2 3
3
3 0 4 5 4 0 9 5 9 0 2 2 3 8 1 2 1
17 12
题意:
以矩阵形式给出n个点所形成的无向图,问进行边权更新后(重边,但边权可能不同),任意两点之间的最短路的和。(样例看了好久。。)
思路:
先跑一次Floyd,然后根据Floyd算法思想,一条边进行更新,那么将影响任意经过这两个点的最短路,那么我们只需要更新所有经过这条边的最短路即可。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int mp[305][305];
int main(){
int n;
while(scanf("%d", &n)!=EOF){
memset(mp, 0x3f, sizeof(mp));
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
scanf("%d", &mp[i][j]);
}
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]);
}
}
}
int cnt, a, b, c;
vector<long long> ans;
scanf("%d", &cnt);
for(int h=0; h<cnt; h++){
scanf("%d%d%d", &a, &b, &c);
if(mp[a-1][b-1]>c) mp[a-1][b-1] = mp[b-1][a-1] = c;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
mp[i][j] = min(mp[i][j], mp[i][a-1]+mp[a-1][j]);
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
mp[i][j] = min(mp[i][j], mp[i][b-1]+mp[b-1][j]);
}
}
long long sum = 0;
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
sum += (long long)mp[i][j];
}
}
ans.push_back(sum);
}
for(int i=0; i<ans.size(); i++){
if(!i)
cout<<ans[i];
else
cout<<" "<<ans[i];
}
cout<<endl;
}
return 0;
}