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. Android getRunningTasks和getRunningAppProcesses失效

    Android 5.0以上的getRunningTasks失效,该方法可以获得在前台运行的系统进程.可以用getRunningAppProcesses方法暂时替代. android6.0以上的getR ...

  2. Android Studio 导入 AOSP 源码

    有了 AOSP 源码,接下来就是如何看了,可以直接文本看,可以用 Source Insight,我当然选择 Android Studio,Android Studio 是我熟悉且十分强大的工具.问题来 ...

  3. C#获取农历的日期(转)

    //C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...

  4. Lubuntu , ubuntu 搜索文件

    使用命令行方式搜索 $ locate your_filename

  5. MySQL入门很简单: 6 视图

    1. 视图含义作用 视图是虚拟的表,是从数据率中一个或多个表中导出来的表:  数据库中只存放了视图的定义,没有存放视图中的数据,数据在原先的表中:  一旦表中的数据发生变化,显示在视图中的数据也会发生 ...

  6. ABAP和Java SpringBoot的单元测试

    ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...

  7. php5.5.15注释问题PHP Deprecated: Comments starting with '#' are deprecated in *.ini 警告解决办法

    PHP Deprecated:  Comments starting with '#' are deprecated in D:\mvam\php5\php.ini on line 1944 in U ...

  8. http协议,tcp协议,ip协议,dns服务之前的关系和区别

    长期以来都有一个问题,大家都在说http协议,tcp协议,ip协议,他们之间到底什么区别,有什么用,没人告诉我,最近看了这本<图解http>明白了一些,以下图片摘自这本书 一.理解一个传输 ...

  9. 深入浅出Nginx

    深入浅出Nginx   文章源自zfz_linux_boy   前言 Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 上图基 ...

  10. P1316 丢瓶盖

    题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...