-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloyd_warshall.cpp
More file actions
30 lines (26 loc) · 818 Bytes
/
floyd_warshall.cpp
File metadata and controls
30 lines (26 loc) · 818 Bytes
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
const ll INF = 1e16;
int main()
{
int n, m, q;
cin >> n >> m;
vector<vll> dist(n, vll(n, INF));
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
dist[a][b] = min((ll)c, dist[a][b]);
}
for (int i = 0; i < n; i++) dist[i][i] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (dist[i][k] != INF && dist[k][j] != INF)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
// negative cycle detection
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
if (dist[i][k] != INF && dist[k][k] < 0 && dist[k][j] != INF)
dist[i][j] = -INF;
}