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 ...
随机推荐
- Core篇——初探Core的认证,授权机制
目录 1.Cookie-based认证的实现 2.Jwt Token 的认证与授权 3.Identity Authentication + EF 的认证 Cookie-based认证的实现 cooki ...
- 微信小程序中获取高度及设备的方法
由于js中可以采用操纵dom的方法来获取页面元素的高度,可是在微信小程序中不能操纵dom,经过查找之后发现仅仅只有以下几个方法可以获取到高度 wx.getSystemInfoSync().window ...
- C++函数的高级特性——小结
相对于C语言,C++增加了重载(overload).内联(inline).const和virtual四种新机制. 1 重载 只能靠参数列表而不能紧靠返回值类型的不同来区分重载函数.编译器根据参数列表为 ...
- Reflection (computer programming) -反射-自身结构信息
n computer science, reflection is the ability of a computer program to examine, introspect, and modi ...
- C++下面关于字符串数组的一些操作
今天在写一个搜索引擎的分词系统,是很简单的那种,但是居然费了我一天的时间还没完成,晚上估计还得弄一会了,但是在这个过程中,遇到了集中关于字符串数组的操作,值得和大家分享一下. 首先是关于统计字符串数组 ...
- v-model指令后面跟的参数(number、lazy、debounce)
1. number 想将用户的输入自动转换为Number类型(如果原值的转换结果为NaN, 则返回原值) 2. lazy 在默认情况下, v-model在input事件中同步输入框的值和数据, 我们可 ...
- [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring
题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...
- [读书笔记] Python 数据分析 (十一)经济和金融数据应用
resample: 重采样函数,可以按照时间来提高或者降低采样频率,fill_method可以使用不同的填充方式. pandas.data_range 的freq参数枚举: Alias Descrip ...
- 基于element的表单渲染器 (el-form-renderer)
基于 element-ui 封装的表单渲染器,完整继承了 element 的属性定义,并进行了简单扩展,从而用户能够通过使用一段预设的数据渲染出一个完整的 element 表单. 演示地址 项目地址 ...
- 2019-03-22 Python Scrapy 入门教程 笔记
Python Scrapy 入门教程 入门教程笔记: # 创建mySpider scrapy startproject mySpider # 创建itcast.py cd C:\Users\theDa ...