Roads in Berland

Roads in Berland

Floyed 变种,图论

题意

给你\(n\) 个城市之间最短路的邻接矩阵,再给你\(k\) 个修改的路径

问你每次修改之后,最短路的总和是多少

思路

对于每次修改,我们都遍历整个图,看看修改过的道路是否能缩短其他两点之间的距离

如果能缩短,就用总的最短路距离减去他们的差值

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define endl '\n'
const int N = 400;
int g[N][N],n,m;
LL sum = 0;
void modify(int a,int b,int c) {
    if(g[a][b] > c) {
        sum -= g[a][b] - c;
        g[a][b] = g[b][a] = c;
    }
    return ;
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,k,a,b,c;
    cin >> n;
    for(int i = 1;i <= n; ++i)
        for(int j = 1;j <= n; ++j) {
            cin >> g[i][j];
            if(i > j) sum += g[i][j];
        }
    cin >> k;
    while(k --) {
        cin >> a >> b >> c;
        modify(a,b,c);
        for(int i = 1;i <= n; ++i) {
            for(int j = 1;j <= n; ++j) {
                modify(i,j,g[i][a] + c + g[b][j]);
            }
        }
        cout << sum << endl;
    }
    return 0;
}

原文链接: https://www.cnblogs.com/lukelmouse/p/12421733.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Roads in Berland

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/371313

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月3日 上午10:42
下一篇 2023年3月3日 上午10:43

相关推荐