zoukankan      html  css  js  c++  java
  • Day4

    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 aibici (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

    Input
    2
    0 5
    5 0
    1
    1 2 3
    Output
    3 
    Input
    3
    0 4 5
    4 0 9
    5 9 0
    2
    2 3 8
    1 2 1
    Output
    17 12 

    思路:要求每一对的最短路,直接想到floyd,但是每次都全跑一次一定会超时,那就每次把修改的边作为中间点进行floyd即可,代码如下:
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    const int maxm = 305;
    
    int G[maxm][maxm]; 
    int N, K;
    
    void calculate() {
        for(int k = 1; k <= N; ++k)
            for(int i = 1; i <= N; ++i)
                for(int j = 1; j <= N; ++j)
                    G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
    }
    
    int main() {
        scanf("%d", &N);
        int t;
        for(int i = 1; i <= N; ++i)
            for(int j = 1; j <= N; ++j) {
                scanf("%d", &G[i][j]);
            }
        calculate();
        scanf("%d", &K);
        int u, v;
        for(int f = 0; f < K; ++f) {
            scanf("%d%d%d", &u, &v, &t);
            LL ans = 0;
            if(t < G[u][v]) {
                G[u][v] = G[v][u] = t;
                for(int i = 1; i <= N; ++i)
                    for(int j = 1; j <= N; ++j) {
                        G[i][j] = min(G[i][j], min(G[i][u]+G[u][j], G[i][v]+G[v][j]));
                        G[j][i] = G[i][j];
                    }
            }            
            for(int i = 1; i < N; ++i)
                for(int j = i+1; j <= N; ++j)
                    ans += G[i][j];
            printf("%I64d ", ans);
        }
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    菜鸟合作伙伴日志接入规范之C#实现
    使用JS在textarea在光标处插入内容
    ASP.NET MVC API 接口验证
    CSS3 grayscale滤镜+SVG使图片变黑白实例页面
    .NET MVC 获取 当前请求的 控制器/视图/区域 的名字
    asp.net 后台获取flv视频地址进行播放【转】
    实现输出h264直播流的rtmp服务器 flash直播服务器【转】
    如何实现一个c/s模式的flv视频点播系统
    视频流服务器配置[windows平台][转]
    小计
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12175407.html
Copyright © 2011-2022 走看看