zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

    C. Roads in Berland
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 thatdi, 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.

    Sample test(s)
    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 

     题目大意:给出一个矩阵,表示一个图和边权值(这里的边权值已经是两点间的最短距离)。然后给出K次修改,对于每一次边权修改,输出修改后任意两点的最小距离的和。

    Floyd变形。首先任意两点的最小距离的和只能暴力求解,对于边的更新,我们肯定会想到跑一边floyd但这会超时。

    对于第修改的边a,b我们可以知道任意两点i,j的最短距离要么经过a-b,要么经过b-a,要么不经过a点也不经过b点。比n^3要好一些.

    dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c);
    dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);
    /* ***********************************************
    Author        :pk29
    Created Time  :2015/8/23 8:53:43
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    int dis[310][310];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n,k;
        cin>>n;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                scanf("%d",&dis[i][j]);
            }
        cin>>k;
        int a,b,c;
        while(k--){
            scanf("%d%d%d",&a,&b,&c);
            if(c<dis[a][b]){
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++){
                        dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c);
                        dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);
                    }
            }
            ll sum=0;
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++){
                    sum+=dis[i][j];
                }
            cout<<sum<<" ";
        }
        return 0;
    }
     
  • 相关阅读:
    HDU 5213 分块 容斥
    HDU 2298 三分
    HDU 5144 三分
    HDU 5145 分块 莫队
    HDU 3938 并查集
    HDU 3926 并查集 图同构简单判断 STL
    POJ 2431 优先队列
    HDU 1811 拓扑排序 并查集
    HDU 2685 GCD推导
    HDU 4496 并查集 逆向思维
  • 原文地址:https://www.cnblogs.com/pk28/p/4755736.html
Copyright © 2011-2022 走看看