C. Roads in Berland

561 阅读3分钟

题目链接:[codeforces.com/problemset/…]

题目 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.

Examples inputCopy 2 0 5 5 0 1 1 2 3 outputCopy 3 inputCopy 3 0 4 5

题意: 给你一个顶点为n的无向图,问你每对顶点之间的最短距离之和是多少。有k次修改,每次修改输出最短距离。

思路: 求每对顶点之间的最短路容易得知需要floyd,然后状态转移的时候进行修改即可。

代码:

#include<bits/stdc++.h>
#define LL long long

using namespace std;
const int maxn=500;
int dist[maxn][maxn];
LL ans;
void modify(int u,int v,int w)
{
    if(dist[u][v]>w)
    {
        ans-=(dist[v][u]-w);
        dist[u][v]=w;
    }
    return;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>dist[i][j];
            if(j>i)
            {
                ans+=dist[i][j];
            }
        }

    }
    //cout<<ans<<endl;
    int k;
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        modify(u,v,w);
        if(dist[u][v]!=dist[v][u])
        {
            dist[v][u]=dist[u][v];
        }
        //cout<<ans<<endl;
        for(int p=1;p<=n;p++)
        {
            for(int q=1;q<=n;q++)
            {
                modify(p,q,dist[p][u]+dist[v][q]+w);
                if(dist[p][q]!=dist[q][p])
                {
                    dist[q][p]=dist[p][u]+dist[v][q]+w;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

个人总结: 经典算法熟练后一般变形,不会出现很大问题,每个算法不能只能知道模板。

个人分类: 图论