-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBellmanFor.cpp
More file actions
89 lines (78 loc) · 1.63 KB
/
Copy pathBellmanFor.cpp
File metadata and controls
89 lines (78 loc) · 1.63 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define X first
#define Y second
struct Bellman_Ford
{
const ll INF = 1e18;
struct edge
{
int v, w;
edge(int v, int w)
: v(v), w(w)
{
}
bool operator<(const edge &e) const
{
return w < e.w;
}
};
int n;
bool neg_cycle = false;
vector<vector<edge>> adj;
vector<ll> dist;
Bellman_Ford(int n)
: n(n), adj(n)
{
}
void add_edge(int u, int v, int w, bool directed = true)
{
adj[u].push_back(edge(v, w));
if (!directed)
adj[v].push_back(edge(u, w));
}
void bellman_ford(int src)
{
dist.assign(n, INF);
dist[src] = 0;
for (int i = 1; i < n; ++i)
{
for (int u = 0; u < n; ++u)
{
for (auto e : adj[u])
{
if (dist[u] + e.w < dist[e.v])
dist[e.v] = dist[u] + e.w;
}
}
}
for (int u = 0; u < n; ++u)
{
for (auto e : adj[u])
{
if (dist[u] + e.w < dist[e.v])
{
neg_cycle = true;
return;
}
}
}
}
};
void solve()
{
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; ++i)
{
solve();
}
}