-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path4.flowdwarshall.cpp
More file actions
78 lines (65 loc) · 1.32 KB
/
Copy path4.flowdwarshall.cpp
File metadata and controls
78 lines (65 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int matrix[N][N];
int next[N][N];
void floydwarshall(int n)
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if(matrix[i][j] > matrix[i][k]+matrix[k][j])
{
matrix[i][j] = matrix[i][k] + matrix[k][j];
next[i][j] = next[i][k];
}
}
}
}
}
void path(int n1, int n2)
{
int i = n1;
int j = n2;
while(j != n2)
{
j = next[i][j];
cout << "j ";
}
}
int main()
{
cout << "Enter nodes number\n";
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if(i == j)
matrix [i][j] = 0;
else
matrix[i][j] = INT_MAX;
next[i][j] = j;
}
}
cout << "Enter edge numbers\n";
int e;
cin >> e;
for (int i = 0; i < e; i++)
{
int a, b, w;
cin >> a >> b >> w;
matrix[a][b] = w;
}
floydwarshall(n);
cout << "Enter node 1 to node 2 for calculate distance\n";
int n1, n2;
cin >> n1 >> n2;
cout << matrix[n1][n2] << endl;
path(n1,n2);
return 0;
}