[CF25C] Roads in Berland - 最短路

Description

给定一张无向图,现在知道了任意两个城市之间的最短路径,现在有 k 个询问,想知道每新增一条道路后所有最短路径的总长度。\(n<=300, k<=300\)

Solution

对于每个询问,我们暴力枚举一遍所有点对 i,j,比较 \(d(i,j)\)\(d(i,a)+c+d(b,j)\) 以及 \(d(i,b)+c+d(a,j)\),如果小于就更新

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N = 305;

int n, k, d[N][N], a, b, c;

signed main()
{
    ios::sync_with_stdio(false);

    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> d[i][j];

    int total_dist = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j < i; j++)
            total_dist += d[i][j];

    cin >> k;
    for (int t = 1; t <= k; t++)
    {
        cin >> a >> b >> c;
        int dist = total_dist;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j < i; j++)
            {
                int original = d[i][j];
                int cross = min(d[i][a] + c + d[b][j], d[i][b] + c + d[a][j]);
                if (cross < original)
                    dist += cross - original, d[i][j] += cross - original, d[j][i] += cross - original;
            }
        cout << dist << " ";
        total_dist = dist;
    }
}

相关文章:

  • 2021-11-17
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2021-06-12
  • 2021-12-22
猜你喜欢
  • 2022-03-09
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2021-12-01
  • 2021-11-05
  • 2022-12-23
相关资源
相似解决方案