CF 179(div2)D(floyed)

来源:互联网 发布:axure7.0 mac 中文版 编辑:程序博客网 时间:2024/05/18 01:58
D. Greg and Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 numberxi 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 thatd(i, v, u) is the shortest path between verticesv andu in the graph that formed before deleting vertexxi, 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 lineaij(1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertexi to vertexj.

The next line contains n distinct integers:x1, x2, ..., xn(1 ≤ xi ≤ 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 inC++. It is preferred to use thecin,cout streams of the%I64d specifier.

Sample test(s)
Input
101
Output
0 
Input
20 54 01 2
Output
9 0 
Input
40 3 1 16 0 400 12 4 0 11 1 1 04 1 2 3
Output
 
这道题去年暑假集训喵呜大神出过,直接暴力肯定会超时,关键是要理解floyed动态规划的本质。
dp[i][j][k] 表示i到j的只经过前k个点做中间节点的最短距离,则第k个点,要么用上,或者没用上
所以 dp[i][j][k] = min(dp[i][j][k-1],dp[i][k][k]+dp[k][j][k]);
再用滚动数组把状态压到二维,k放在最外层的循环。
而这道题是逐渐删点,可以到过来求,就是逐渐加点的过程,用一个vis标记当前点是否加入
然后每新加入一个点,就相当于原来floyed里的k加了一个,这样总的算法就还是O(n^3)
注意新进来一个点,必须先更新以这个点为首尾点到其他点的最短距离,然后再更新任意两点以这个点为中间节点的最短距离
详见代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define LL long longusing namespace std;const int maxn = 500 + 5;LL M[maxn][maxn];int vis[maxn],x[maxn];LL ans[maxn];int main(){    int n;    while(scanf("%d",&n) != EOF){        memset(vis,0,sizeof(vis));        for(int i = 1;i <= n;i++)            for(int j = 1;j <= n;j++)                cin >> M[i][j];        for(int i = 0;i < n;i++){            cin >> x[i];        }        for(int u = n-1;u >= 0;u--){            vis[x[u]] = 1;            int k = x[u];            LL sum = 0;            //先更新新加入的点到其他点的最短距离            for(int i = 1;i <= n;i++){                for(int j = 1;j <= n;j++){                    if(vis[i] && vis[j]){                        M[k][i] = min(M[k][i],M[k][j]+M[j][i]);                        M[i][k] = min(M[i][k],M[i][j]+M[j][k]);                    }                }            }            //再更新其他任意两点间,以新加入点为中间点的的最短距离            for(int i = 1;i <= n;i++)                for(int j = 1;j <= n;j++)                    if(vis[i] && vis[j])                        M[i][j] = min(M[i][j],M[i][k]+M[k][j]);            //最后累加            for(int i = 1;i <= n;i++)                for(int j = 1;j <= n;j++)                    if(vis[i] && vis[j])                        sum += M[i][j];            ans[u] = sum;        }        for(int i = 0;i < n-1;i++) cout << ans[i] << ' ';        cout << ans[n-1] << endl;    }    return 0;}