题意:
已经给你最短路的邻接矩阵,然后每次给你一条路,连续求出每个点之间的最短路总和。
POINT:
即判断每次能不能进行松弛,因为给你一条路,只能松弛有关这两个点的路。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const 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);
}
}
}