那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
3 seconds
256 megabytes
standard input
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.
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 ≤ xi ≤ n) — the vertices that Greg deletes.
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.
1
0
1
0
2
0 5
4 0
1 2
9 0
4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3
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算法的更多相关文章
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- [CodeForces - 296D]Greg and Graph(floyd)
Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...
- CodeForces 295B Greg and Graph (floyd+离线)
<题目链接> 题目大意:给定$n$个点的有向完全带权图$(n\leq500)$,现在进行$n$次操作,每次操作从图中删除一个点(每删除一个点,都会将与它相关联的边都删除),问你每次删点之前 ...
- Codeforce 295B Greg and Graph(Floyd的深入理解)
题目链接:http://codeforces.com/problemset/problem/295/B 题目大意:给出n个点的完全有权有向图,每次删去一个点,求删掉该点之前整张图各个点的最短路之和(包 ...
- 295B - Greg and Graph (floyd逆序处理)
题意:给出任意两点之间的距离,然后逐个删除这些点和与点相连的边,问,在每次删除前的所有点对的最短距离之和 分析:首先想到的是floyd,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前 ...
- 那些年我们写过的T-SQL(上篇)
在当今这个多种不同数据库混用,各种不同语言不同框架融合的年代(一切为了降低成本并高效的提供服务),知识点多如牛毛.虽然大部分SQL脚本可以使用标准SQL来写,但在实际中,效率就是一切,因而每种不同厂商 ...
- 那些年我们写过的T-SQL(中篇)
中篇的重点在于,在复杂情况下使用表表达式的查询,尤其是公用表表达式(CTE),也就是非常方便的WITH AS XXX的应用,在SQL代码,这种方式至少可以提高一倍的工作效率.此外开窗函数ROW_NUM ...
- 那些年我们写过的T-SQL(下篇)
下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有?到目前为止很多大型传统企业仍然很依赖存储过程.这部分主要难理解的部分是事务和锁机制这块,本文会进行简单的阐述.虽然很 ...
- 那些年我们写过的T-SQL(下篇)(转)
原文:http://www.cnblogs.com/wanliwang01/p/TSQL_Base04.html 下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...
随机推荐
- jQuery中find()和filter()的区别
filter(expr):筛选出与指定表达式匹配的元素的元素集合,其中expr可以是多个选择器的组合.是对自身集合元素的筛选. find()会在元素内寻找匹配元素,而filter()是筛选元素:是对它 ...
- 专题合集:深入Android媒体存储服务
Android 有一套媒体存储服务,进程名是 android.process.media,主要负责把磁盘中的文件信息保存到数据库当中,供其他 APP 使用以及 MTP 模式使用.这里包含了数据库管理. ...
- 在Centos 5.x或6.x上安装RHEL EPEL Repo
本文介绍了如何在CentOS 5.x或者CentOS 6.x的系统上使用Fedora Epel repos一个第三方repo:remi资源库.这些资源包并不是天然地支持CentOS,但是提供了很多流行 ...
- iOS6和iOS7代码的适配(4)——tableView
iOS7上不少控件的样子有了变化(毕竟要扁平化嘛),不过感觉变化最大的肯定非tableView莫属.因为这个控件的高度可定制性,原先是使用及其广泛的,这样的一个改变自然也影响颇大. 1.accesso ...
- app.listen(3000)与app是不一样的
前者是server代码如下 Server { domain: null, _events: { request: { [Function] domain: undefined, _events: [O ...
- 神器之“c#反编译工具“
1. ".net reflector"本人正在使用,下载地址 2."ILSpy"没实用过,可是听杨中科老师说挺好. 有了这个,妈妈再也不用操心我看不到别人封装好 ...
- Eclipse无法识别(手机)设备的解决方案
遇到问题 开始学习android一个多月了,用Eclipse开发,用android手机调试.之前一直好好的,突然Eclipse无法识别手机设备了.纠结了好久,找了各种解决方法,弄了一晚上终于解决问题了 ...
- Android Navigation Drawer(导航抽屉)
Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Navigation ...
- c#高级语言编程(第一部分)
1.一步一步学c#(一):.NET体系结构 2.一步一步学c#(二):核心c# 3.一步一步学c#(三):对象和类型 4.一步一步学c#(四):继承 5.一步一步学c#(五):泛型 6.一步一步学c# ...
- JavaScript之面向对象学习一
1.通过Object构造函数和对象字面量来创建对象缺点:使用同一个接口创建很多的对象,会产生大量的重复代码.比如我需要创建人的对象,并且需要三类人,医生.工程师.老师,他们可以抽象出很多属性,比如姓名 ...