那些年我们写过的三重循环----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 下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...
随机推荐
- 网站linux.linuxidc.com有很多好资料
免费下载地址在 http://linux.linuxidc.com/ 用户名与密码都是www.linuxidc.com 有一些介绍:www.linuxidc.com/download
- zendstudio -chinese
http://archive.eclipse.org/technology/babel/index.php http://www.eclipse.org/babel/downloads.php 注册码 ...
- MongoDB(二)
通过程序来操作数据库: //链接字符串 string connectionString = "mongodb://192.168.1.107"; //数据库名 string dat ...
- C#.NET中的CTS、CLS和CLR
以下内容来自:http://www.cnblogs.com/zagelover/articles/2741370.html 在学习.NET的过程中,都会不可避免地接触到这三个概念,那么这三个东西是什么 ...
- React和Angular
React和Angular 你若装逼,请带我飞! 从前,从前,听说React只负责UI,话说写Angular代码就像写后端,现在看来,React赢在情怀上了: 我认为没必要老是拿React和Angul ...
- 无线功率 mW 和 dBm 的换算
无线电发射机输出的射频信号,通过馈线(电缆)输送到天线,由天线以电磁波形式辐射出去.电磁波到达接收地点后,由天线接收下来(仅仅接收很小很小一部分功率),并通过馈线送到无线电接收机.因此在无线网络的工程 ...
- c++多线程编程之互斥对象(锁)的使用之----死锁
一.死锁会在什么情况发生 1.假设有如下代码 mutex; //代表一个全局互斥对象 void A() { mutex.lock(); //这里操作共享数据 B(); //这里调用B方法 mu ...
- 从麦肯锡到小黑裙-Project Gravitas |华丽志
从麦肯锡到小黑裙-Project Gravitas |华丽志 从麦肯锡到小黑裙-Project Gravitas
- Qt编写端口扫描工具
Qt提供了QTcpSocket类,可以方便地建立TCP连接.利用这一功能,Maxiee编写了一个简单地端口扫描工具. 软件的功能就是,扫描某一网段的固定端口,如80端口,若目的地址开放了这一端口,那么 ...
- 填充Z形二维数组
形如 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16 的数组称谓Z形二维数组.填充这样的数组其实只要按照Z形进行行走填充即可,设置一个flag指示方向,行 ...