CF295B.Greg and Graph

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

  • The game consists of n steps.
  • On the i-th step Greg removes vertex number x__i from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.
  • Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex x__i, then Greg wants to know the value of the following sum: .

Help Greg, print the value of the required sum before each step.

格雷格有一个带权有向图,包含 nn 个顶点。该图中,任意两个不同的顶点之间均存在双向边(即:对任意 u≠vu \ne v,既有边 u→vu \to v,也有边 v→uv \to u)。格雷格喜欢和这个图一起玩耍,现在他发明了一个新游戏:

  • 游戏共进行 nn 步。
  • 在第 ii 步中,格雷格将从图中移除编号为 xix_i 的顶点;在移除该顶点的同时,所有与之关联的入边和出边也一并被移除。
  • 在执行每一步操作之前,格雷格希望知道:当前图中所有剩余顶点对之间的最短路径长度之和。最短路径可以经过任意剩余顶点。换言之,若记 d(i,v,u)d(i, v, u) 为在删除顶点 xix_i 之前所形成的图中顶点 vv 到 uu 的最短路径长度,则格雷格希望知道如下和式的值:

请帮助格雷格,在每一步操作开始前输出该和式的值。

输入格式

The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.

Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line a__ij (1 ≤ a__ij ≤ 105, a__ii = 0) represents the weight of the edge that goes from vertex i to vertex j.

The next line contains n distinct integers: _x_1, _x_2, ..., x__n (1 ≤ x__i ≤ n) — the vertices that Greg deletes.

第一行包含一个整数 $ n (( 1 \leq n \leq 500 $)—— 图中顶点的数量。

接下来的 $ n $ 行,每行包含 $ n $ 个整数 —— 图的邻接矩阵:第 $ i $ 行中的第 $ j $ 个数 $ a_{ij} (( 1 \leq a_{ij} \leq 10^5 $,且 $ a_{ii} = 0 $)表示从顶点 $ i $ 指向顶点 $ j $ 的边的权重。

下一行包含 $ n $ 个互不相同的整数:$ x_1,,x_2,,\dots,,x_n (( 1 \leq x_i \leq n $)—— Greg 将要删除的顶点。

输出格式

Print n integers — the i-th number equals the required sum before the i-th step.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

输出 n 个整数——第 i 个数等于第 i 步之前的所需和。

请注意,在 C++ 中读写 64 位整数时,请勿使用 %lld 说明符。推荐使用 cin、cout 流或 %I64d 说明符。

输入输出样例

  • 输入#1

    1
    0
    1

    输出#1

    0
  • 输入#2

    2
    0 5
    4 0
    1 2

    输出#2

    9 0
  • 输入#3

    4
    0 3 1 1
    6 0 400 1
    2 4 0 1
    1 1 1 0
    4 1 2 3

    输出#3

    17 23 404 0

输入解题思路,AI测评打分。不知道怎么写?

首页