forked from No-Idle/graph-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacencymatrix.cpp
79 lines (71 loc) · 1.84 KB
/
adjacencymatrix.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
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
void floyd(int **, int);
class AdjMatrix {
private:
void operator = (const AdjMatrix &)
{}
public:
int **g;
int n;
AgjMatrix(int _n) {
if (_n > 20000) // very rarely you have more than 512MB
throw exception("'_n' is too large for matrix creating!");
n = _n;
g = new int*[n];
for (int i = 0; i < n; i++)
g[i] = new int[n];
}
~AdjMatrix() {
for (int i = 0; i < n; i++)
delete[] g[i];
delete[] g;
}
void floyd()
/**
* if you want to save original weights, you should copy the graph
*/
{
floyd(g, n);
}
};
void floyd(int **g, int n)
/**
* g - adjacency matrix, n - vertex count
* each edge of type 'i to i' has 0-weight
*/
{
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
void floyd_with_path(int **g, int n) {
int **next = new int*[n];
for (int i = 0; i < n; i++)
next[i] = new int[n];
for (int k = 0; k < n; k++) i
for (int i = 0; i < n; i++) u
for (int j = 0; j < n; j++) v
if (g[i][k] + g[k][j] < g[i][j]) {
g[i][j] = g[i][k] + g[k][j];
next[i][j] = next[i][k];
}
return next;
}
void readAdjMatOr(AdjMatrix &g) {
int n;
cin >> n;
g = AgjMatrix(n);
for (int i = 0, a, b, w; i < n; i++) {
cin >> a >> b >> w;
g.g[a][b] = w;
}
}
void readAdjMatNOr(AdjMatrix &g) {
int n;
cin >> n;
g = AgjMatrix(n);
for (int i = 0, a, b, w; i < n; i++) {
cin >> a >> b >> w;
g.g[a][b] = g.g[b][a] = w;
}
}