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

All Pairs Shortest Path

Input

An edge-weighted graph G (VE).

|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 2147483647 struct edge{
int from,to,cost;
}; edge es[]; int d[][]; // d[i][j]表示点i到点j的最短路径 int V,E; //点和边的数量 //判断负圈
bool find_negative_loop(){
int s[];
fill(s,s+V,); for(int i = ;i < V; i++){
for(int j = ;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-) return false;
}
}
}
return true;
} //任意两点间最短路径
void warshall_floyd(){ for(int k = ;k < V; k++){
for(int i = ;i < V; i++){
for(int j = ;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 = ;i < V; i++){
for(int j = ;j < V; j++){
d[i][j] = INF;
}
d[i][i] = ;
}
for(int i = ;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 = ;i < V; i++){
for(int j = ;j < V; j++){
if(j != ) cout << " ";
if(d[i][j] == INF) cout << "INF";
else cout << d[i][j];
}
cout << endl;
}
}else{
cout <<"NEGATIVE CYCLE" <<endl;
} return ;
}

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

  1. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  2. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  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. HDU3631:Shortest Path(Floyd)

    Problem Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in ...

  5. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  6. [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

  7. [MIT6.006] 19. Daynamic Programming I: Fibonacci, Shortest Path 动态规划I:斐波那契,最短路径

    这节课讲动态规划的内容,动态规划是一种通用且有效的算法设计思路,它的主要成分是"子问题"+"重用".它可以用于斐波那契和最短路径等问题的求解上. 一.斐波那契 ...

  8. Floyd算法——计算图中任意两点之间的最短路径

    百度百科定义:传送门 一.floyd算法 说实话这个算法是用来求多源最短路径的算法. 算法原理: 1,从任意一条单边路径开始.所有两点之间的距离是边的权,如果两点之间没有边相连,则权为无穷大. 2,对 ...

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

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

随机推荐

  1. ES6的基本语法

    ES6 详细参考页面 简介 ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现.一般来说,这两个词是可以互换的. let命令 ES6新增了let命令,用来声明变 ...

  2. [ios] 如何调用其他app h5界面调用打开app

    参考资料:app唤醒app h5唤醒app 有趣的URL Scheme 被唤起端需要做的工作(demoApp): 1.设置URL Scheme  只是一个app的标识  具体是什么自己定  一个Sch ...

  3. JAVA-mysql读写分离插件介绍

    kingshard是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基本的读写分离的功能上,致力于简化MySQL分库分表操作:能够让DBA通过kingshard轻松平滑地实现M ...

  4. APUE学习笔记7——进程间通信

    1 管道 管道一般是一种半双工的进程间通信方式,只能够在具有公共祖先的进程之间使用,比如一个管道由一个进程创建,然后该进程调用fork,之后父.子进程就可以使用该管道. 管道是调用pipe函数创建的. ...

  5. ZBrush中Document特性介绍

    ZBrush®中的Document调色板用于加载或保存ZBrush文档,导入背景图像.导出背景图像.调整画布大小和设置背景颜色.本文小编来给大家介绍下Document常用的一些基本功能. ZBrush ...

  6. Pyhton学习——Day11

    # Python中的内部模块# 函数学习的意义:抽取重复代码# 模块:不用重复写,模块及py文件,提高了代码的可维护性,其次,编写代码不必从零开始,当一个模块编写完毕,不必再重复编写# import ...

  7. 算法21----重塑矩阵 LeetCode566

    1.题目 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重 ...

  8. tcpsock.v2 与 ecocache

    因为很不满意 tcpsock 的设计与实现,及有意专为譬如游戏服务器端开发设计一套 TCP 网络库,所以年初即有了 tcpsock.v2 的开发计划,于是粗略整理出了以下几条目标计划: 1) TcpC ...

  9. Coding for Speed 技术分享

    上周和公司技术同事们作了次<Coding for Speed>技术分享,本来这只是再普通不过的技术探讨和交流(虽然挂了个颇有噱头的名称),但分享的时候逻辑没理好,语速很快,时间也太紧,因此 ...

  10. 用Arcade表达式添加标签

    Arcade表达式是轻量级的脚本语言,我们可以通过全局变量$feature获取要素属性.比如说,要为城市添加标签,利用CITY_NAME列,我们可以编写语句:$feature.CITY_NAME.Ar ...