Description

题意:给定一个有向图,一共有N个点,给邻接矩阵。依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点之前的所有两点间最短距离和。n<=500

Solution

如果暴力打肯定是会超时的,那就要运用到floyd(hj)

floyd算法内2个循环就相当于新加入外循环的那个点然后跟新最短路,

所以可以把题目看成倒过来依次加点,每次\(n^2\)平方更新一下,总共\(O(n^3)\)

Code

#include <cstdio>
#include <algorithm>
#define ll long long
#define N 510
using namespace std; ll Ans[N],g[N][N];
int p[N],n;
bool vis[N]; int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)scanf("%lld",&g[i][j]);
for(int i=1;i<=n;++i)scanf("%d",&p[i]);
for(int i=n,now;i>=1;--i){
vis[now=p[i]]=1;
for(int j=1;j<=n;++j)
for(int k=1;k<=n;++k)
g[j][k]=min(g[j][k],g[j][now]+g[now][k]);
for(int j=1;j<=n;++j)
for(int k=1;k<=n;++k)
if(vis[j]&&vis[k]) Ans[i]+=g[j][k];
}
for(int i=1;i<=n;++i) printf("%lld ",Ans[i]);
return 0;
}

[CodeForces - 296D]Greg and Graph(floyd)的更多相关文章

  1. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Graph(Floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  3. Graph (floyd)

    Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the oppo ...

  4. Codeforces 295D - Greg and Caves(dp)

    题意: 给出一个 \(n \times m\) 的矩阵,需对其进行黑白染色,使得以下条件成立: 存在区间 \([l,r]\)(\(1\leq l\leq r\leq n\)),使得第 \(l,l+1, ...

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

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

  6. WUSTOJ 1326: Graph(Java)费马数

    题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...

  7. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

  8. codeforces 295C Greg and Friends(BFS+DP)

    One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...

  9. (floyd)佛洛伊德算法

    Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...

随机推荐

  1. css float布局--右侧上部固定下部自适应大小

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. sharepoint2007就地升级2010系列(三)升级系统

    OK,上两篇我们完成sharepoint2007的预览,以及升级前的补丁准备.今天我们来正式进行升级windows server系统以及SQL数据库 升级之前首先确定 search服务停止而且被禁用, ...

  3. Java学习笔记——String类常用方法

    所谓的字符串就是对数组的包装,所以字符串内容无法进行更改.String在JDK1.8以前保存的是字符数组:private final char value[]; 而在JDK1.9之后保存的是字节数组: ...

  4. 百度开源项目插件 - Echarts 图表

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. TP5.1:依赖注入、绑定一个类到容器里、绑定一个闭包到容器中

    依赖注入 1.在application中创建一个文件夹,名字为commom,commom文件夹中创建被注入文件夹,在被注入文件夹中创建一个名为demo.php的文件 2.在demo.php中输入: 3 ...

  6. simotion byte/word ASCII码转换为字符、字符串

    建立string类型,和byte类型(ASCII)的数据 将byte类型(ASCII)赋值给string中的一个字符 参考程序 VAR_GLOBAL myword :WORD; mystring :S ...

  7. 如何处理用代码创建SD Sales order时遇到的错误消息KI 180

    错误消息KI 180:You must enter a company code for transaction Create sales document 代码: REPORT zcreate_so ...

  8. an exception occurred while initializing the database.

    对于手动删除本地的LocalDB数据库之后出现标题所示异常的,推荐下面的命令: sqllocaldb.exe stop v11.0 sqllocaldb.exe delete v11.0 在程序包管理 ...

  9. 原生Servlet 上传文件

    依赖jar <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons ...

  10. matlab 下载

    Obrazy DVD programu Matlab ke stažení Verze 2015b Verze 2015a Verze 2014b Verze 2014a Verze 2012b Ve ...