Roads in Berland(图论)

Description

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.

Sample Input

Input

20 55 011 2 3

Output

3 

Input

30 4 54 0 95 9 022 3 81 2 1

Output

17 12 

题目的大概意思是有n个城市,现给出这n个城市之间没两个城市的距离,改变一些城市的距离,问最后所有这些路径长度最小之和。
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long long n,m,a[310][310],sum,t1,t2,s;
    while (cin>>n)
    {
          sum=0;
          for (int i=1;i<=n;i++)
          for (int j=1;j<=n;j++)
          {
              cin>>a[i][j];
               sum+=a[i][j];
          }
          sum/=2;  //没条路算了两次,多以要除以2。
          cin>>m;
          for (int p=1;p<=m;p++)
          {
              cin>>t1>>t2>>s;
              if (a[t1][t2]<s)
              {
                    cout <<sum<<endl;
                    continue;
              }
              for (int i=1;i<=n;i++)   //检查一下改变一条路之后对其它路有没有影响
              for (int j=1;j<=n;j++)
              {
                  long long temp=a[i][t1]+s+a[t2][j];
                  if (temp<a[i][j])    //如果改变t1到t2的距离,看看i到t1再到t2再到i的距离是否比i直接到j的距离短,是的话则改变i到j的距离,同时改变j到i的距离。
                  {
                       sum=sum-(a[i][j]-temp);
                       a[i][j]=temp;
                       a[j][i]=temp;
                  }
              }
              cout <<sum<<endl;
          }
    }
    return 0;
}

  

时间: 2024-10-11 03:30:25

Roads in Berland(图论)的相关文章

CodeForces 567E President and Roads(最短路 + tarjan)

CodeForces 567E President and Roads Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s?≠?t). The cities are connected by one-way roads, the travel time for each of the road

【CodeForces 567E】President and Roads(最短路)

Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year t

CF 191C Fools and Roads lca 或者 树链剖分

They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities th

codeforces 723F : st-Spanning Tree

Description There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible tha

2016NEFU集训第n+3场 E - New Reform

Description Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only th

CodeForces 659E New Reform

题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 #include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #inc

XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondMemory limit: 512 mebibytesThe civil war in Berland continues for five years already. The United Nation decided to end the bloodshed.Berland consists o

cf246 ENew Reform (并查集找环)

Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing r

Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will enti