-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFloyd_Warshall.cpp
More file actions
59 lines (51 loc) · 1.01 KB
/
Floyd_Warshall.cpp
File metadata and controls
59 lines (51 loc) · 1.01 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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define lli long long int
#define MAXN 10000
#define INF 999999
int n , m , a , b , c ;
int dist [MAXN][MAXN] ;
void floyd_warshall ()
{
for (int k = 0 ; k < n ; k++)
{
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]) ;
}
}
}
}
void initialize ()
{
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
if (i == j)
{
dist[i][j] = 0 ;
}
else
{
dist[i][j] = INF ;
}
}
}
}
int main()
{
cin >> n >> m ;
initialize () ;
for (int i = 0 ; i < m ; i++)
{
cin >> a >> b >> c ;
dist [a][b] = min (dist[a][b] , c) ;
dist [b][a] = min (dist[b][a] , c) ;
}
floyd_warshall () ;
return 0;
}