那些年我们写过的三重循环----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 下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...
随机推荐
- Windows2008RT搭建VPN服务器
总结一下2008系统搭建VPN的步骤和过程,自己有个人网站和服务要通过互联网发布出来.服务器放在自己家里,宽带是民用的.也就产生了服务发布的一些问题.用无法映射出真实的公网IP,或是一些其他内部的问题 ...
- 手把手教程 Surface如何进行系统恢复?
手把手教程 Surface如何进行系统恢复? 2015-01-29 05:53:00 [ 中关村在线 原创 ] 作者: 周博林 | 责编:周博林 收藏文章 分享到 评论(10) Windo ...
- 如何在 静态编译的QT 5.5.1 中 使用数据库插件连接 ODBC(调用静态插件)
前段时间由于工作的关系,需要编写一个将数据插入到 Sql server 2012 的桌面软件. 由于之前使用的是MFC,偶然间接触到了Qt后,被它的简洁惊艳到了,于是便毅然而然的转投到了Qt的怀抱,哈 ...
- Windows Azure 社区新闻综述(#71 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 · 使用 Azure ...
- Cpp again
1,
- 驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的一次记6分。 答案:错误 2013《123号令-附件2》一、机动车驾驶人有下列违法行为之一,一次记12分[重新考《科目一》]:(七)驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的; 可以参考:http://zhinan.jxedt.com/info/6375.htm
这一组交通警察手势是什么信号?_2600602 交警的面部对着哪个方向就是在指挥哪个方向的车,减速慢行是右手 左转弯待转是左手!~ 哎 本题解释由台州交通驾校提供 4755支持 hmq 只能看 ...
- CodeForces 135C C. Zero-One
题目 题意: 一个01串,AB两个人轮流删去一个字符,直到只剩两个,A先手.最后剩的两位组成一个二进制数,A要使其最小,B要使其最大. 有一些部分不知道原来是什么,用?表示,求所有的可能里,最后剩下的 ...
- Numpy之ndarray与matrix
1. ndarray对象 ndarray是numpy中的一个N维数组对象,可以进行矢量算术运算,它是一个通用的同构数据多维容器,即其中的所有元素必须是相同类型的. 可以使用array函数创建数组,每个 ...
- SVN使用Tips
1. 如果在本地删除了某个文件,在Cornerstone上的本地仓库会出现D的标志,并且文件不存在. 这时,只需要将该文件提交到服务器上,本地仓库就会清除了已删除的文件的标识,同时,服务器上也会自动删 ...
- 深度学习工具caffe具体安装指南
caffe安装指南-吐血整理 前言: 在一台系统环境较好的linux机器上能够非常easy的安装caffe,可是假设系统本身非常旧,又没有GPU的话.安装就太麻烦了,全部都得从头做起,本文档旨在尽可能 ...