题目链接:

  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B

Single Source Shortest Path (Negative Edges)

Input
An edge-weighted graph G (V, E) and the source r.

|V| |E| r
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. r is the source of the graph.

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) which is reachable from the source r, print

NEGATIVE CYCLE
in a line.

Otherwise, print

c0
c1
:
c|V|−1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, … |V|−1 in order. If there is no path from the source to a vertex, print “INF”.

Constraints
1 ≤ |V| ≤ 1000
0 ≤ |E| ≤ 2000
-10000 ≤ di ≤ 10000
There are no parallel edges
There are no self-loops
Sample Input 1
4 5 0
0 1 2
0 2 3
1 2 -5
1 3 1
2 3 2
Sample Output 1
0
2
-3
-1

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

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

这题求单源最短路径+负圈,用Bellman-Ford算法。

注意:这里求的负圈附带了条件,就是源点r能触及到的负圈。

Bellman-Ford算法+求负圈链接

代码:

#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[2010];  //存储边 int d[1010];  // d[i] 表示点i离源点的最短距离 int V,E; //点和边的数量 bool shortest_path(int s){fill(d,d+V,INF);d[s] = 0;int v = 0;while(true){bool update = false;for(int i = 0;i < E; i++){edge e = es[i];if(d[e.from] != INF && d[e.to] > d[e.from] + e.cost){d[e.to] = d[e.from] + e.cost;update = true;if(v == V-1) return true;}}if(!update) break;v++;}return false;
}int main(){int r;cin >> V >> E >> r;for(int i = 0;i < E; i++) cin >> es[i].from >> es[i].to >>es[i].cost;if(!shortest_path(r)){for(int i = 0;i < V; i++){if(d[i] == INF) cout <<"INF" <<endl;else cout << d[i] << endl;}}else{cout <<"NEGATIVE CYCLE" <<endl;}return 0;
} 

AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)相关推荐

  1. 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 ...

  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_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

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

  4. 4kyu Path Finder #2: shortest path

    4kyu Path Finder #2: shortest path 题目背景: Task You are at position [0, 0] in maze NxN and you can onl ...

  5. ROS Base path和Source space不一致问题,修改文件名后无法make问题,catkin_make报错问题

    在一次在ROS 的学习实践中,将Ros工程目录名称更改了,source后,出现了base path和Source space不一致问题: 报错提示: Base path: /home/pot/catk ...

  6. go mod init错误(go: cannot determine module path for source directory)

    在go的项目中执行go mo init的时候会报一下错误 go: cannot determine module path for source directory F:\gowork\test5 ( ...

  7. go: cannot determine module path for source directory D:\go\code\SipSvrProxy\src\Common (outside GOP

    大家好,我是躺平哥! 错误 分享一个初学者容易犯的错误. 我们在使用go  mod 的时候,可能会爆一下错误 go: cannot determine module path for source d ...

  8. go: cannot determine module path for source directory

    解决方法如下: ​ xxxxxxx\example>go mod init go: cannot determine module path for source directory xxxxx ...

  9. linux给PATH添加变量,给PATH变量添加路径--用Enki学Linux系列(6)

    给PATH变量添加路径Adding directories to your $PATH PATH是一个告诉shell在哪些路径中搜索可执行文件来响应用户的命令的环境变量. PATH is an env ...

最新文章

  1. 机器学习笔记(七)贝叶斯分类器
  2. pthread属性使用(转)
  3. 教你如何察觉出网络钓鱼电子邮件
  4. javascript 核心概念(1)-数据类型
  5. ElasticSearch 聚合查询
  6. 新编计算机英语,新编计算机英语教程
  7. mysql有则修改无则添加_通过sql实现无则插入有则修改(MySQL)
  8. ansys icem cfd网格划分技术实例详解_详解航空燃油滑油3D打印热交换器设计流程...
  9. serv-u启动管理控制台后提示脚本错误解决方案
  10. 英文文本处理 c github_真香警告!有了这个搜索大法,GitHub可以玩到飞起来!
  11. Adobe软件字体导入无法显示的问题
  12. php paypal 订单查询,php – 成功付款后如何从paypal获取交易详情
  13. No signature of method: build_*.android() is applicable for argument types
  14. 计网 - 内容分发网络 : CDN 回源的工作机制初探
  15. ME525做网络收音机和学外文用了……(安卓4.4.4系统,20190817更新)
  16. zuiqingchun4
  17. Minecraft作弊端介绍:PYRO CLIENT-一个平凡但神秘的存在
  18. 常用API、static、数组复制、双色球练习与酒店管理系统
  19. 微信小程序 全局字体控制(字体大小)
  20. 3个iPhone设置让你成为“人类高质量iPhone用户”

热门文章

  1. linux中here文档,Linux下Bash Heredoc(Here document)的用法及基本示例
  2. Java黑皮书课后题第9章:9.1 (Rectangle类)遵照9.2节中Circle类的例子,设计一个名为Rectangle的类表示矩形
  3. 软件体系结构课后作业03
  4. 阿里巴巴集团2014秋季校园招聘笔试题
  5. 随机数范围扩展方法总结
  6. SQL Server学习之路(五):“增删改查”之“改”
  7. grunt前端构建工具使用教程
  8. WORD2010自动编号后,目录那里编号和文字中间有很大的空格,怎么
  9. “天昌”的挽歌——逝去的背影(一)
  10. [系统安全] 三十一.恶意代码检测(1)恶意代码攻击溯源及恶意样本分析