那些年我们写过的三重循环----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 下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...
随机推荐
- 移动端js插件分享
分享几个移动端使用的插件,个人感觉挺不错的. 1. TouchSlide1.1 滑动的焦点图 http://pan.baidu.com/s/1i3J6bbB 2. iscroll.js 模拟滚动条 ...
- LeakCanary,检测安卓,java内存泄漏
官方中文API地址:http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/
- Html内容超出标记宽度后自动隐藏
我们在显示长文本时,往往需要去在C#端去截取字符,但这绝对不是一个好方面,因为我们的长文本往往都是代HTML标记的,你一个载不好,就会出现乱码问题(出现半个HTML标记),而比较好的作法就是通过CSS ...
- U盘装centos7系统过程
1. 使用最新版UltraISO将ISO镜像刻录到U盘 一定要是最新版,试用版都可以,按下图操作: 2. U盘启动电脑进入安装界面 正常情况下你应该会看到下面的这个界面: 选择第一项,然后按TAB键, ...
- QT连接mysql中文显示问题
亲测OK! #vim /etc/mysql/my.cnf [mysqld]下面加入: default-character-set=utf8 重启mysql /etc/init.d/mysql rest ...
- vim note
2016-1-22 vim plugin collections: (参考 https://www.youtube.com/watch?v=0QFR-_wUoA0) vim-pathogen 插件管 ...
- Umbraco模型默认属性
Media Model的属性: umbracoFileumbracoWidthumbracoHeightumbracoBytesumbracoExtension
- 转:script中的async和defer
script中的async和defer defer: This Boolean attribute is set to indicate to a browser that the script is ...
- 如何在Delphi中调用VC6.0开发的COM
上次写了如何在VC6.0下对Delphi写的COM进行调用,原本想马上写如何在Delphi中调用VC6.0开发的COM时,由于在写事例程序中碰到了个很怪的问题,在我机子上用VC写的接口程序编译能通过. ...
- 【FZU】2152 文件系统
Problem 2152 文件系统 Accept: 63 Submit: 126 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...