-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellman-Ford.cpp
More file actions
77 lines (67 loc) · 1.21 KB
/
Copy pathBellman-Ford.cpp
File metadata and controls
77 lines (67 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <limits.h>
#include <math.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define CLR(a) memset((a), 0 ,sizeof(a))
//
// Bellman-Ford algorithm
//
int inf = INT_MAX / 2;
int edge[V] = {inf};
int V,E;//V is the number of edges. E is that of vertexes.
vector<PII> vertex[V];
void BF()
{
bool update=true;
int to,dist;
while(update)
{
update = false;
REP(i,V)
if(edge[i]!=inf)
for(int j = 0;j < vertex[i].size();j++)
{
to = vertex[i][j].first;
dist = vertex[i][j].second;
if(edge[to] > edge[i] + dist)
{
edge[to] = edge[i] + dist;
update = true;
}
}
}
}
bool BF_negative()
{
bool update=true;
int to,dist,count;
while(update)
{
update = false;
REP(i,V)
if(edge[i]!=inf)
for(int j = 0;j < vertex[i].size();j++)
{
to = vertex[i][j].first;
dist = vertex[i][j].second;
if(edge[to] > edge[i] + dist)
{
edge[to] = edge[i] + dist;
update = true;
}
}
count ++;
if(count > V * E)
return false;
}
return true;
}