题目链接:

  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 (VE) 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 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算法求负圈和单源最短路径)的更多相关文章

  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. 单源最短距离 Single Source Shortest Path

    单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...

  3. JAVA之单源最短路径(Single Source Shortest Path,SSSP问题)dijkstra算法求解

    题目简介:给定一个带权有向图,再给定图中一个顶点(源点),求该点到其他所有点的最短距离,称为单源最短路径问题. 如下图,求点1到其他各点的最短距离 准备工作:以下为该题所需要用到的数据 int N; ...

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

  5. eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过

    问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...

  6. eclipse debug时老提示edit source lookup path解决方案

    用myeclipse debug web应用的时候,总提示edit source lookup path,每次都得手动选择项目,费时费力.在网上终于找到了方法. 搬运:http://www.educi ...

  7. 报错: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 ...

  8. debug找到source lookup path以及,debug跑到另外的解决办法

    在我们使用eclipse调试的时候,有时候会出一些奇葩的问题,比如找不到Source  lookup path, 这时我们可以点击Edit Source Lookup Path.接着回弹出一个 我们只 ...

  9. eclipse debug的时候提示debug Edit Source Lookup path

    原因可能是代码资源包未加载到debug的路径中,解决方法如下: Debug 视图下 ->在调试的线程上 右键单击 ->选择Edit Source Lookup Path ->选择Ad ...

随机推荐

  1. 使用右键打开Visual Code

    Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\Visual Code]@="Edit with Visual ...

  2. ROS中安装任意版本的OPENCV

    转自:http://tieba.baidu.com/p/5023000237 安装 Opencv 3.2 on Ubuntu 16.04 并创建node测试 step 1: 安装一些package s ...

  3. BZOJ 3720 树分块

    借鉴了别人的代码-- //By SiriusRen #include <cmath> #include <cstdio> #include <cstring> #i ...

  4. tp5数据库操作 Db类

    一.链接数据库 1.配置文件定义  application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...

  5. Java操作Kafka执行不成功

    使用kafka-clients操作kafka始终不成功,原因不清楚,下面贴出相关代码及配置,请懂得指点一下,谢谢! 环境及依赖 <dependency> <groupId>or ...

  6. 学习ZBrush到底需不需要用数位板?

    在学习ZBrush时,要控制下笔的力度,而这一点是鼠标办不到的.这时就需要拥有一块手绘板.手绘板可以控制笔刷的力度. 在雕刻之前,要先来了解CG设计领域广泛应用的硬件产品—数位板,如图所示. 数位板又 ...

  7. 树莓派使用samba

    tips:打算利用树莓派做局域网内的文件共享服务器, 实测发现树莓派挂载一个硬盘比较合适,挂载多个硬盘则会出现供电不足而挂载失败, 即使利用外置供电接入多个硬盘实测效果也不好,树莓派在IO上无法处理多 ...

  8. Pyhton学习——Day9

    #文件操作的其他方法 ###############################################################################文件的内置操作函数# ...

  9. 一篇文章彻底弄懂Base64编码原理(转载)

    在互联网中的每一刻,你可能都在享受着Base64带来的便捷,但对于Base64的基础原理又了解多少?今天这篇博文带领大家了解一下Base64的底层实现. Base64的由来 目前Base64已经成为网 ...

  10. CF1037E Trips (离线+图上构造)

    题目大意:一共有n个人,每天早上会有两个人成为朋友,朋友关系不具有传递性,晚上,它们会组织旅游,如果一个人去旅游,那么他不少于$k$个朋友也要和他去旅游,求每天的最大旅游人数 一开始并没有想到反向建图 ...