往图中增加一条边,求修改后的图的最短路径...
floyd算法是,不过的通过第三个点更新图的最短路径,O(n*n*n);
由此我们受到启发,我们可以把边进行拆分,拆分成两个点,通过这两个点,不断地去更新其他的边..
因为如果新边能够影响到其他边,那么势必其他的要经过这个点,,DP的策略是经不经过该点....
算法果然博大精深啊....ORZ
首先,如果直接暴力的话肯定 T(因为是 o(n4) 的时间复杂度)
我们需要注意的是,每次只是新建一条边
令 g[i][j] 表示顶点 i 和顶点 j 之间的最短距离
假设新建的边是 a---b 边权为 c
如果 g[a][b]<=c 那么,新建的边不会用来更新多有顶点之间的最短距离,这时候直接统计最短距离和输出就行,下面讨论 g[a][b]>c 的情况
首先 g[a][b]=g[b][a]=c 这是肯定的了
由于只是新加了边,原来图中的边还存在(虽然我们不知道这些边的具体情况),那么我们可以这么做,还是利用 Floyd 算法的思想
先求出 a 到其他所有点的最短距离 g[i][a] (由于 g[a][i]=g[i][a],我们只讨论一种情况)
g[i][a] 要么不经过 a---b 这条边,要么经过这条边,于是很好解决了:
g[i][a]=g[a][i]=min( g[i][a], g[i][b]+g[b][a] )
同理,求出 b 到其他所有的点的最短距离:
g[i][b]=g[b][i]=min( g[i][b], g[i][a]+g[a][b] )
最后,对于任意两个点 i 和 j,他们的最短路,要么经过 a,要么经过 b,要么还是原来的值,那么
g[i][j]=min(g[i][j], min(g[i][a]+g[a][j], g[i][b]+g[b][j]) )
这样,任意两个点之间的最短路就求出来了,然后暴力统计就行了
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 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
#include<bits/stdc++.h> using namespace std; const int N =333; int f[N][N]; int main() { int n,i,j; long long ans=0; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cin>>f[i][j]; } int m,u,v,r; cin>>m; while(m--) { ans=0; cin>>u>>v>>r; if(f[u][v]>r) { f[u][v]=f[v][u]=r; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) f[i][j]=min(f[i][j],f[i][u]+f[u][j]); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { f[i][j]=min(f[i][j],f[i][v]+f[v][j]); } } } for(i=1;i<=n;i++) { for(j=i;j<=n;j++) ans+=f[i][j]; } cout<<ans<<endl; } return 0; }