forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Johnson'sAlgorithm.cpp
51 lines (47 loc) · 1.03 KB
/
Johnson'sAlgorithm.cpp
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
#include<iostream>
#include<conio.h>
using namespace std;
int min(int a, int b);
int cost[10][10], a[10][10], i, j, k, c;
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
int main(int argc, char **argv)
{
int n, m;
cout << "Enter no of vertices";
cin >> n;
cout << "Enter no of edges";
cin >> m;
cout << "Enter the\nEDGE Cost\n";
for (k = 1; k <= m; k++)
{
cin >> i >> j >> c;
a[i][j] = cost[i][j] = c;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
if (a[i][j] == 0 && i != j)
a[i][j] = 31999;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
cout << "Resultant adj matrix\n";
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (a[i][j] != 31999)
cout << a[i][j] << " ";
}
cout << "\n";
}
return 0;
}