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 ...
随机推荐
- 在js在添版本号
为了增加用户访问网站体验,快速打开网页,许多网站都对不常更新的js,css文件在浏览器端设置了缓存.但如果在服务器端做了更新,浏览器使用的仍是缓存在本地的js文件,除非强制清缓存(ctrl+F5).为 ...
- JS实现掷骰子
JS实现掷骰子 实现方法: 方法一:通过background-position.background-image.backg-repeat三个属性以及jquery animate()方法改变背景骰子图 ...
- P2216 [HAOI2007]理想的正方形(二维RMQ)
题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...
- gdal集成kml库的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取.我发现gdal是支持集成kml库的.不过集成这个km ...
- 自己写的PHP的mql类
用类封装的一个数据库的操作,不仅安全,而且会省去很多代码. header('Content-type:text/html;charset="utf-8"'); class mysq ...
- Codeforces Round #499 (Div. 2) C.FLY 数学推导_逆推
本题应该是可以使用实数二分的,不过笔者一直未调出来,而且发现了一种更为优美的解法,那就是逆推. 首先,不难猜到在最优解中当飞船回到 111 号节点时油量一定为 000, 这就意味着减少的油量等于减少之 ...
- Pyhton学习——Day8
###########################################max函数#################################################### ...
- 使用Git--将本地项目提交到Github
前置工作 1. 在GitHub官网注册一个GitHub账号: 2. 安装git工具,在Git官网下载对应版本的Git: 方法一: 1. 进入Github首页,点击New repository新建一个项 ...
- UVA340-Master-Mind Hints(紫书例题3.4)
MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Break ...
- myeclipse如何取消某一个文件的校验