CodeForces

删除正向不好处理,转为逆向,从最后一个删除的点开始插入

 

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.

Input

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.

Output

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.

Examples

Input
1
0
1
Output
0 
Input
2
0 5
4 0
1 2
Output
9 0 
Input
4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3
Output
17 23 404 0 
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<vector>
 5 #include<map>
 6 #include<cmath>
 7 #include<cstdio>
 8 #include<queue>
 9 typedef long long ll;
10 const int mod = 1e9+7;
11 const int maxn = 1000000;
12 const int inf = 0x3f3f3f3f;
13 using namespace std;
14 //因为我是蒟蒻 全Int wa了 test30  所以全开ll 
15 ll mp[510][510];
16 ll delet[510];
17 ll vis[510];
18 ll ans[510];
19 ll n;
20 ll cnt=1;
21 int main()
22 {
23      ios::sync_with_stdio(false);
24     cin.tie(0);cout.tie(0);
25     cin>>n;
26     for(ll i=1;i<=n;i++)
27     {
28         for(ll j=1;j<=n;j++)
29         {
30             cin>>mp[i][j];
31         }
32     }
33     
34     for(ll i=1;i<=n;i++)
35     {
36         cin>>delet[i]; 
37     }
38     
39     for(ll i = n;i >= 1;i--)/*从最后一个删除点插入*/ 
40 {
41         ll mid = delet[i];
42         
43         vis[mid]=1;             
44         
45         for(ll j=1;j<=n;j++)
46         {
47             for(ll k=1;k<=n;k++)
48             {
49                 mp[j][k] = min(mp[j][k],mp[j][mid]+mp[mid][k]);/*以删除的点作为中间点 floyd*/
50             }
51         }
52         ll sum=0;
53         for(ll j=1;j<=n;j++)
54         {
55             for(ll k=1;k<=n;k++)
56             {
57                 if(vis[j] && vis[k])
58                 {
59                     sum+=mp[j][k];            /*计算权值*/ 
60                 }
61             }
62         }
63         ans[cnt++] = sum;
64 }
65     
66     
67     for(ll i = n ; i > 0 ;i--)
68     {
69         cout<<ans[i]<<" ";
70     }
71     cout<<endl;
72     
73 }
原文地址:https://www.cnblogs.com/lightWh1te/p/13376563.html