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 number xi 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 xi, 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 aij(1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j.

The next line contains n distinct integers: x1, x2, ..., xn (1 ≤ xin) — 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 thecin, cout streams of the %I64d specifier.

Sample test(s)
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 

题目链接:here

非常值得一做的经典题目,让我们再一次想起Floyd算法三重for循环背后的光芒。

#include<cstdio>
#define N 505
#define ll long long int ll dp[N][N],arr[N],ans[N],n; ll _min(ll a,ll b) {
return a<b?a:b;
} int main()
{
while(scanf("%d",&n)!=EOF) {
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) scanf("%lld",&dp[i][j]);
}
for(int i=0;i<n;i++) scanf("%lld",&arr[n-i-1]);
for(int i=0;i<n;i++) arr[i]--;
for(int mid=0;mid<n;mid++) {
int u=arr[mid];
ll temp=0;
for(int e=0;e<n;e++) {
for(int s=0;s<n;s++) {
int a=arr[s];
int b=arr[e];
dp[a][b]=_min(dp[a][b],dp[a][u]+dp[u][b]);
if(s<=mid && e<=mid) temp+=dp[a][b];
}
}
ans[n-mid-1]=temp;
}
for(int i=0;i<n;i++) printf("%I64d ",ans[i]);
printf("\n");
}
return 0;
}

那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法的更多相关文章

  1. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  2. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  3. CodeForces 295B Greg and Graph (floyd+离线)

    <题目链接> 题目大意:给定$n$个点的有向完全带权图$(n\leq500)$,现在进行$n$次操作,每次操作从图中删除一个点(每删除一个点,都会将与它相关联的边都删除),问你每次删点之前 ...

  4. Codeforce 295B Greg and Graph(Floyd的深入理解)

    题目链接:http://codeforces.com/problemset/problem/295/B 题目大意:给出n个点的完全有权有向图,每次删去一个点,求删掉该点之前整张图各个点的最短路之和(包 ...

  5. 295B - Greg and Graph (floyd逆序处理)

    题意:给出任意两点之间的距离,然后逐个删除这些点和与点相连的边,问,在每次删除前的所有点对的最短距离之和 分析:首先想到的是floyd,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前 ...

  6. 那些年我们写过的T-SQL(上篇)

    在当今这个多种不同数据库混用,各种不同语言不同框架融合的年代(一切为了降低成本并高效的提供服务),知识点多如牛毛.虽然大部分SQL脚本可以使用标准SQL来写,但在实际中,效率就是一切,因而每种不同厂商 ...

  7. 那些年我们写过的T-SQL(中篇)

    中篇的重点在于,在复杂情况下使用表表达式的查询,尤其是公用表表达式(CTE),也就是非常方便的WITH AS XXX的应用,在SQL代码,这种方式至少可以提高一倍的工作效率.此外开窗函数ROW_NUM ...

  8. 那些年我们写过的T-SQL(下篇)

    下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有?到目前为止很多大型传统企业仍然很依赖存储过程.这部分主要难理解的部分是事务和锁机制这块,本文会进行简单的阐述.虽然很 ...

  9. 那些年我们写过的T-SQL(下篇)(转)

    原文:http://www.cnblogs.com/wanliwang01/p/TSQL_Base04.html   下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...

随机推荐

  1. $.browser.msie jq解析不出来的原因及解决方法

    检查是否为 IE6:// Oldif ($.browser.msie && 7 > $.browser.version) {}// Newif ('undefined' == t ...

  2. Js把URL中的参数解析为一个对象

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. Firefox中打开DOM Inspector

    一: 二:ctrl+shift+I

  4. C#学习日志 day 5 plus------ interface 数组及stringBuilder相关

    interface 接口interface可以理解为两个程序达成的协议. 实际就是一个留给后续开发的框架.若想继承这个interface,就必须实现interface规定的 函数及结构等.一般会以大写 ...

  5. English interview!

    Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...

  6. QT中代码中与设计器中控件信号与SLOT连接(原来还可以这样连接)

    双击testqt.ui 托一个push button到窗体中,双击,可以输入字符 按F4或 menu->edit->edit signals/slots 定义SLOT 选择已定义好的SLO ...

  7. 在VC6.0下如何调用Delphi5.0开发的进程内COM

    因为本人的语言水平很差,考大学时150的总分,我考了个60分.外语也是,初中及格过一次,会考及格过一次.其它的时间好像从没有及格过.所以我不写文章,因我一百字的文章给我写,至少要出八九个错别字.哈哈… ...

  8. POJ 2799 IP Networks

    network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...

  9. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  10. c#搭建服务端 准备工作(1)

    思路 搭建服务器主要为了接收客户端所传来的数据,在学习过程中,整体的搭建逻辑大体分为以下几个步骤: 1.启动线程监听服务端口 2.监听客户端链接并进行处理 3.接收客户端传入的消息 4.向客户端回传( ...