AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)
题目链接:
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能触及到的负圈。
代码:
#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] 表示点i离源点的最短距离 int V,E; //点和边的数量 bool shortest_path(int s){
fill(d,d+V,INF); d[s] = ; int v = ;
while(true){
bool update = false;
for(int i = ;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-) return true;
}
}
if(!update) break;
v++;
}
return false;
} int main(){ int r;
cin >> V >> E >> r; for(int i = ;i < E; i++) cin >> es[i].from >> es[i].to >>es[i].cost; if(!shortest_path(r)){
for(int i = ;i < V; i++){
if(d[i] == INF) cout <<"INF" <<endl;
else cout << d[i] << endl;
}
}else{
cout <<"NEGATIVE CYCLE" <<endl;
} return ;
}
AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)的更多相关文章
- 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 ...
- 单源最短距离 Single Source Shortest Path
单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...
- JAVA之单源最短路径(Single Source Shortest Path,SSSP问题)dijkstra算法求解
题目简介:给定一个带权有向图,再给定图中一个顶点(源点),求该点到其他所有点的最短距离,称为单源最短路径问题. 如下图,求点1到其他各点的最短距离 准备工作:以下为该题所需要用到的数据 int N; ...
- 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 ...
- eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...
- eclipse debug时老提示edit source lookup path解决方案
用myeclipse debug web应用的时候,总提示edit source lookup path,每次都得手动选择项目,费时费力.在网上终于找到了方法. 搬运:http://www.educi ...
- 报错:Can't find a source file at "xxxxx“Locate the file or edit the source lookup path to include its location.
调试问题: Can't find a source file at "/tmp/TI_MKLIB6sLCzz/SRC/exit.c" Locate the file or edit ...
- debug找到source lookup path以及,debug跑到另外的解决办法
在我们使用eclipse调试的时候,有时候会出一些奇葩的问题,比如找不到Source lookup path, 这时我们可以点击Edit Source Lookup Path.接着回弹出一个 我们只 ...
- eclipse debug的时候提示debug Edit Source Lookup path
原因可能是代码资源包未加载到debug的路径中,解决方法如下: Debug 视图下 ->在调试的线程上 右键单击 ->选择Edit Source Lookup Path ->选择Ad ...
随机推荐
- Android系统自适应屏幕大小
1.屏幕相关概念1.1分辨率是指屏幕上有横竖各有多少个像素1.2屏幕尺寸指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸android将屏幕大小分为四个级别(smal ...
- HDU 3830 Checkers(二分+lca)
Description Little X, Little Y and Little Z are playing checkers when Little Y is annoyed. So he wan ...
- C语言中以文本方式读写文件时换行符转换的注意事项
我们知道在UNIX下是没有回车符(\r)的,只有换行符(\n),而C语言诞生于UNIX(Linux即面向开源的UNIX,Mac OS也是UNIX发展而来的,而Windows是从MS-DOS发展而来,与 ...
- Android 网络状态变化的监听
1.在流量网页,时实现断网,提醒等功能. 需要权限 <uses-permission android:name="android.permission.INTERNET"/& ...
- 读书笔记第三周 人月神话 刘鼎乾 PB16070837
读书笔记第三周:人月神话 这本书主要讲述了如何管理一个软件开发团队的问题,其中如何提高团队的效率可以说是本书的重点之一了.感觉这本书地中文版翻译得比较晦涩,很多表达比较模糊,看起来有些吃力,因此下 ...
- CheckBox的Attributes
在看老同事写的代码的时候,发现了这样的一段代码:之前自己没有遇到过,记录下吧. 大致是这样的 foreach (GridViewRow grv in GridView1.Rows) { CheckBo ...
- Oracle安装后命令行中运行sqlplus / as sysdba出现错误ora-01031:insufficient privileges
Win10安装Oracle后命令行中运行sqlplus as sysdba出现错误ora-01031insufficient privileges的解决方法 情景描述 错误样例 错误分析 解决方法 情 ...
- 利用UncaughtExceptionHandler捕获未try...catch到的异常
public class test { public static void main(String[] args){ Thread thread = new Thread(new MyThread( ...
- 三、frpc 完整配置文件
# [common] is integral section [common] # A literal address or host name for IPv6 must be enclosed # ...
- 1、koa的安装,get和post方法的基础使用
koa是干什么:koa是用来解决回调嵌套的方案,减少异步回调,提高代码的可读性和可维护性同时也改进了错误处理( Express的错误处理相当糟糕) koa相比express的优点在哪里1.更加优雅简单 ...