Codeforces 296D Greg and Graph【Floyd+逆序思维】好题

来源:互联网 发布:mac查看u盘隐藏文件 编辑:程序博客网 时间:2024/05/23 14:12

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 and u 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 vertex j.

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 the cin, cout streams of the %I64d specifier.

Examples
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
17 23 404 0 

题目大意:

一共有N个点,给你已知的N*N的邻接矩阵。一共去掉N次节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点之前的所有两点间最短距离和。


思路:


本题与hdu 3631有异曲同工之处,有兴趣的小伙伴可以去做一做


1、首先,如果我们正序处理图,然后再处理两点间距离,很显然用Floyd的时间复杂度会高达O(n^4),即使使用堆优化的Dij也是O(n^3logn),都是会超时的。


2、那么我们考虑Floyd的特点:对应删除一个节点,其实就相当于在Floyd过程中不对这个点进行松弛操作,并且在最后累加两点间最短距离和的时候,将相关这个节点的情况去除,就是删除这个节点的两点间最短距离和的求法。


3、那么我们考虑逆序处理,相当于每一次增加一个节点,即对当前这个节点进行一次O(n^2)的松弛操作。然后统计和的时候,将那些已经增加进来的节点的两点间最短距离累加输出即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64ll map[515][515];ll a[515][515];int b[515];int vis[515];ll ans[515];int main(){    int n;    while(~scanf("%d",&n))    {        memset(vis,0,sizeof(vis));        memset(map,0,sizeof(map));        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                scanf("%I64d",&map[i][j]);            }        }        for(int i=1;i<=n;i++)        {            scanf("%d",&b[i]);        }        int cont=0;        for(int i=n;i>=1;i--)        {            int now=b[i];            vis[now]=1;            for(int j=1;j<=n;j++)            {                for(int k=1;k<=n;k++)                {                    map[j][k]=min(map[j][k],map[j][now]+map[now][k]);                }            }            ll sum=0;            for(int j=1;j<=n;j++)            {                for(int k=1;k<=n;k++)                {                    if(vis[j]==0||vis[k]==0)continue;                    sum+=map[j][k];                }            }            ans[cont++]=sum;        }        for(int i=cont-1;i>=0;i--)        {            printf("%I64d ",ans[i]);        }        printf("\n");    }}




0 0
原创粉丝点击