【CF25C】Roads in Berland 【Floyd深入理解】

Roads in Berland

题目链接

传送门

题目概述

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.

就是说n个城市,给你用临接矩阵存的边,使得每两个城市间都有最短路。现有k次更新操作。问每次更新操作之后两个城市间距离最小值之和。(不重复)

测试样例

/*
input:
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
output:
17 12 
 */

分析

用临接矩阵存的图,很容易想到用关键代码只有5行的floyd算法。不过如果更新的操作比较多,每次都跑一遍最短路的话的话很容易超时,我们思考一下如何进行优化。floyd算法时根据是否有一个中转点k,使得u->k->v的距离小于u->v的距离,从而进行更新。根据题目意思,我们一条一条的加边,所以如果新边比原来的最短距离大的话我们根本不需要更新,如果要小的话我们只需要针对新边进行更新,这样可以极大缩减时间复杂度,只需要在开头跑一次完整的floyd算法就OK了。

代码


```cpp
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;
const int N = 305;
int n, k;
int dis[N][N];
long long ans = 0;
inline int read(){
    int x = 0, op = 1;
    char ch = getchar();
    while (!isdigit(ch)){
        if (ch == '-') op = -1;
        ch = getchar();
    }
    while (isdigit(ch)){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * op;
}
 
void solve(){
    int x = read(), y = read(), z = read();
    if (dis[x][y] <= z)
        printf("%lld ",ans);
    else{
        ans = 0;
        dis[x][y] = dis[y][x] = z;
        for (int i = 1; i <= n; ++i)
            for (int j = i + 1; j <= n; ++j) {
                if (dis[i][j] > dis[i][x] + dis[x][y] + dis[y][j])
                    dis[i][j] = dis[j][i] = dis[i][x] + dis[x][y] + dis[y][j];
                
                if (dis[i][j] > dis[i][y] + dis[y][x] + dis[x][j])
                   dis[i][j] = dis[j][i] = dis[i][y] + dis[y][x] + dis[x][j];
                ans += dis[i][j];
            }
        printf("%lld ",ans);
    }
}
 
int main() {
    n = read();
    memset(dis, 0x3f, sizeof(dis));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            dis[i][j] = read();
 
    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if (dis[i][j] > dis[i][k] + dis[k][j])
                    dis[i][j] = dis[i][k] + dis[k][j];
 
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            ans += dis[i][j];
 
    k = read();
    while (k--)
        solve();
    return 0;
}

错误示范

//更新不完全
        ans = 0;
        dis[x][y] = dis[y][x] = z;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j) {
                if (dis[i][j] > dis[i][x] + dis[x][y] + dis[y][j])
                    dis[i][j] = dis[j][i] = dis[i][x] + dis[x][y] + dis[y][j];
                //if (dis[i][j] > dis[i][y] + dis[y][x] + dis[x][j])
                //   dis[i][j] = dis[j][i] = dis[i][y] + dis[y][x] + dis[x][j];
                ans += dis[i][j];
            }
        ans /= 2;

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页