题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C

All Pairs Shortest Path

Input
An edge-weighted graph G (V, E).

|V| |E|
s0 t0 d0
s1 t1 d1
:
s|E|−1 t|E|−1 d|E|−1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,…, |V|−1 respectively.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output
If the graph contains a negative cycle (a cycle whose sum of edge costs is a negative value), print

NEGATIVE CYCLE
in a line.

Otherwise, print

D0,0 D0,1 … D0,|V|−1
D1,0 D1,1 … D1,|V|−1
:
D|V|−1,0 D1,1 … D|V|−1,|V|−1
The output consists of |V| lines. For each ith line, print the cost of the shortest path from vertex i to each vertex j (j=0,1,…|V|−1) respectively. If there is no path from vertex i to vertex j, print “INF”. Print a space between the costs.

Constraints
1 ≤ |V| ≤ 100
0 ≤ |E| ≤ 9900
-2 × 107 ≤ di ≤ 2 × 107
There are no parallel edges
There are no self-loops
Sample Input 1
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 7
Sample Output 1
0 1 3 4
INF 0 2 3
INF INF 0 1
INF INF 7 0

Sample Input 2
4 6
0 1 1
0 2 -5
1 2 2
1 3 4
2 3 1
3 2 7
Sample Output 2
0 1 -5 -4
INF 0 2 3
INF INF 0 1
INF INF 7 0

Sample Input 3
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 -7
Sample Output 3
NEGATIVE CYCLE

这题先用Bellman-Ford算法判断负圈,再用Floyd-Warshall算法求任意两点间的最短路即可。

代码:

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 2147483647struct edge{int from,to,cost;
};edge es[10000];int d[110][110]; // d[i][j]表示点i到点j的最短路径 int V,E; //点和边的数量 //判断负圈
bool find_negative_loop(){int s[110];fill(s,s+V,0);for(int i = 0;i < V; i++){for(int j = 0;j < E; j++){edge e = es[j];if(s[e.to] > s[e.from] + e.cost){s[e.to] = s[e.from] + e.cost;if(i == V-1) return false;}}}return true;
}//任意两点间最短路径
void warshall_floyd(){for(int k = 0;k < V; k++){for(int i = 0;i < V; i++){for(int j = 0;j < V; j++){if(d[i][k] != INF && d[k][j] != INF)d[i][j] = min(d[i][j], d[i][k] + d[k][j]);}}}
}int main(){cin >> V >> E;for(int i = 0;i < V; i++){for(int j = 0;j < V; j++){d[i][j] = INF;}d[i][i] = 0;}for(int i = 0;i < E; i++) cin >> es[i].from >> es[i].to >> es[i].cost,d[es[i].from][es[i].to] = es[i].cost;if(find_negative_loop()){warshall_floyd();for(int i = 0;i < V; i++){for(int j = 0;j < V; j++){if(j != 0) cout << " ";if(d[i][j] == INF) cout << "INF";else cout << d[i][j];}cout << endl;}}else{cout <<"NEGATIVE CYCLE" <<endl;}return 0;
} 

AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)相关推荐

  1. 图论 ---- F. The Shortest Statement (最短路的性质 + 任意两点间最短路 + 图转树)

    题目链接 题目大意: 给你一个nnn个点mmm条边的无向图,就是动态询问任意两点间的最短路 n,m∈[1,1e5],m−n≤20n,m\in[1,1e5],m-n\leq20n,m∈[1,1e5],m ...

  2. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  3. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  4. hdu 5636 Shortest Path(Floyd最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5636 解题思路: 这道题可以用Floyd解决,不过需要特殊处理一下: 实际上我们只需要利用添加的那三条 ...

  5. 弗洛伊德算法c语言path,Floyd算法(弗洛伊德算法)

    算法描述: Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按 ...

  6. 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)

    一.最短路径问题 [google笔试题]一个环形公路,给出相邻两点的距离(一个数组),求任意两点的最短距离,要求空间复杂度不超过O(N). 如果从有向图中某一顶点(称为源点)到达另一顶点(称为终点)的 ...

  7. 847. Shortest Path Visiting All Nodes(三)

    DP 这道题目还可以用动态规划解决.在图论中解决最短路径问题有Dijkstra算法和bellman-ford算法.这道题目也需要用到DP.所以先学习一下这两个算法的思想和区别. 两个算法比较 Dijs ...

  8. Floyd - Warshall(弗洛伊德算法)

    简介:Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系 ...

  9. warshall算法求传递闭包c++_【建模小课堂】图论算法

    图论算法 图论算法在计算机科学中扮演着很重要的角色,它提供了对很多问题都有效的一种简单而系统的建模方式.很多问题都可以转化为图论问题,然后用图论的基本算法加以解决.这类问题算法主要包括Dijkstra ...

最新文章

  1. 高等数学·为什么f``(x)小于0:则f(x)在[a,b]上的图形是凹的。f``(x)大于0:则f(x)在[a,b]上的图形是凸的。
  2. Eclipse 中maven插件坏死解决办法
  3. CSS清除浮动大全共8种方法
  4. php bdecode,PHP base64_encode和base64_decode 编码/解码url
  5. 已放弃 (核心已转储)_辽宁已放弃师弟!CBA公布外援优先续约名单:北京队不在列...
  6. 通过Iframe在A网站页面内嵌入空白页面的方式,跨域获取B网站的数据返回给A网站!...
  7. 教你怎么在vi和vim上查找字符串
  8. 通过pxe远程安装linux,通过PXE远程安装多台Linux系统
  9. uestc 851 方老师与素数
  10. Java8新特性总结 -6.Date/Time API
  11. 团队作业——Alpha冲刺之事后诸葛亮
  12. 解决Linux系统在设置alias命令重启后失效的问题
  13. Appium+Robotframework实现Android应用的自动化测试-3:一个必不可少的工具介绍
  14. Hi,我们的代码重构了
  15. 接入HTTPS,给网站加一把绿色小锁
  16. UE4蓝图基础02-节点的基本知识
  17. 苹果邮箱怎么登录qq邮箱_邮箱格式怎么写 电子邮箱格式怎么写
  18. 【Scikit-Learn 中文文档】40 数据集加载工具 - 用户指南 | ApacheCN
  19. Pytorch基础打卡01
  20. Unity警告 Trying to Invoke method: PlayManager.ReturnTheMainMenu couldn‘t be called.

热门文章

  1. html+form+multipartform-data,表单 – 如何处理node.js中的multipart / form-data
  2. java普通类获取session_springboot普通类中如何获取session?
  3. 作业一 郝树伟 1101210664
  4. 提升【百度网盘】下载速度
  5. Segments POJ 3304 直线与线段是否相交
  6. php 接口继承接口
  7. SqlConnection中 Close()Dispose()Using()的区别
  8. [数据库] MySQL基础知识之日期判断及添加排序序号
  9. C# 系统应用之窗体最小化至任务栏及常用操作
  10. 【数据结构与算法】之深入解析“路径交叉”的求解思路与算法示例