CF295B.Greg and Graph

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Greg has a weighed directed graph, consisting of nn 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 nn steps.
  • On the ii -th step Greg removes vertex number xix_{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)d(i,v,u) is the shortest path between vertices vv and uu in the graph that formed before deleting vertex xix_{i} , then Greg wants to know the value of the following sum: .

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

输入格式

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

Next nn lines contain nn integers each — the graph adjacency matrix: the jj -th number in the ii -th line aija_{ij} (1<=aij<=105,aii=0)(1<=a_{ij}<=10^{5},a_{ii}=0) represents the weight of the edge that goes from vertex ii to vertex jj .

The next line contains nn distinct integers: x1,x2,...,xnx_{1},x_{2},...,x_{n} (1<=xi<=n)(1<=x_{i}<=n) — the vertices that Greg deletes.

输出格式

Print nn integers — the ii -th number equals the required sum before the ii -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.

输入输出样例

  • 输入#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 
首页