-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJohnson's all shortest paths algorithm.cpp
177 lines (144 loc) · 3.07 KB
/
Johnson's all shortest paths algorithm.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <bits/stdc++.h>
/// Jonhson's Algorithm
/// All pair's shortest path problem
/// Complexity: O(V^2 * log(V) + V * E)
using namespace std;
#define mp make_pair
#define pb push_back
#define y1 lalalalalala
#define index lalalalalalala
#define show(a, i) cout << "a[" << i << "] = " << a[i] << endl;
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define ll long long
const int INF = 1e9 + 1;
int getint()
{
char ch;
int neg = 1;
int val = 0;
while(ch = getchar())
{
if(ch == '-') neg = -1;
else if(ch > 57 || ch < 48) break;
else val = 10 * val + ch - 48;
}
val = val * neg;
return val;
}
const int cs = 1e5 + 1;
struct edge
{
int to;
int w;
edge () {
}
};
struct c_edge
{
int from;
int to;
int w;
c_edge () {
}
};
struct node
{
int d;
int vertex;
};
struct cmp
{
bool operator()(node A, node B)
{
return A.d < B.d;
}
};
vector< vector<edge> >v;
vector<c_edge>V;
vector< vi >dis;
vi h, used;
priority_queue<node, vector<node>, cmp>Q;
int n, m;
void relax(c_edge x)
{
if(h[x.from] != INT_MAX)
h[x.to] = min(h[x.to], h[x.from] + x.w);
}
bool Bellman_Ford()
{
for(int i = 1 ; i <= n ; i ++)
V.pb({n + 1, i, 0});
for(int i = 1 ; i <= n ; i ++)
h[i] = INT_MAX;
h[n + 1] = 0;
for(int i = 1 ; i <= n ; i ++)
for(auto j : V)
relax(j);
for(auto j : V)
if(h[j.to] > h[j.from] + j.w)
return true;
return false;
}
void init_Dijkstra(int vertex)
{
while(!Q.empty())
Q.pop();
dis[vertex].resize(n + 1, INT_MAX);
}
void Dijkstra(int vertex)
{
init_Dijkstra(vertex);
dis[vertex][vertex] = 0;
Q.push({0, vertex});
while(!Q.empty())
{
node current = Q.top();
Q.pop();
for(auto i : v[current.vertex])
{
if(dis[vertex][i.to] > current.d + i.w)
{
dis[vertex][i.to] = current.d + i.w;
Q.push({dis[vertex][i.to], i.to});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(false);
cin >> n >> m;
V.resize(m);
h.resize(n + 2);
dis.resize(n + 2);
v.resize(n + 1);
for(int i = 0 ; i < m ; i ++)
cin >> V[i].from >> V[i].to >> V[i].w;
if( Bellman_Ford() == true)
{
cout << "negative cycle" << endl;
return 0;
}
for(auto i : V)
{
if(i.from == n + 1)
continue;
v[i.from].pb({i.to, i.w + h[i.from] - h[i.to]});
}
for(int i = 1 ; i <= n ; i ++)
Dijkstra(i);
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= n ; j ++)
dis[i][j] = dis[i][j] + h[j] - h[i];
cout << endl;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
cout << dis[i][j] << " ";
cout << endl;
}
///cerr << "Elapsed time: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec" << endl;
return 0;
}