-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJohnson's Algorithm.cpp
169 lines (158 loc) · 3.4 KB
/
Johnson's 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
/*input
6
2 2
1 2 -2
2 1 1
6 8
1 2 8
1 6 6
6 2 3
2 3 -1
3 6 -2
6 5 -2
5 4 2
3 4 3
4 4
1 2 1
2 3 2
3 4 3
4 1 0
2 0
1 0
2 2
1 2 -1
2 1 0
*/
// https://www.spoj.com/problems/JHNSN/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define dd double
#define pb push_back
#define mp make_pair
const ll N = 1001;
ll n, m, dist[N], vis[N], H[N];
vector<pair<ll, ll > > graph[N];
vector<pair<pair<ll, ll>, ll > > edges;
ll shortest_path[N][N];
void clear_everything()
{
edges.clear();
for(ll i = 0; i <= n; i++)
{
graph[i].clear();
dist[i] = 1E12;
vis[i] = 0;
H[i] = 0;
}
for(ll i = 0; i <= n; i++)
for(ll j = 0; j <= n; j++)
shortest_path[i][j] = 1E12;
}
bool bellman_check(ll edge_idx)
{
ll u = edges[edge_idx].first.first;
ll v = edges[edge_idx].first.second;
ll w = edges[edge_idx].second;
if(dist[v] > (dist[u] + w))
{
dist[v] = dist[u] + w;
return true;
}
return false;
}
bool bellman_ford(ll src)
{
dist[src] = 0;
for(ll i = 0; i <= (n + 1); i++)
for(ll j = 0; j < edges.size(); j++)
if(bellman_check(j) && i == (n+1))
return true;
return false;
}
void update_weight_values()
{
graph[n].clear();
for(ll i = 0; i < n; i++)
{
for(ll j = 0; j < graph[i].size(); j++)
{
ll u = i;
ll v = graph[i][j].first;
graph[i][j].second += (dist[u] - dist[v]);
}
}
for(ll i = 0; i <= n; i++) H[i] = dist[i], cout << dist[i] << " ";
cout << endl << endl;
}
void dijkstra(ll src)
{
for(ll i = 0; i < n; i++) vis[i] = 0, dist[i] = 1E12;
dist[src] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > pq;
pq.push({(ll)0, src});
while(!pq.empty())
{
ll u = pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto k : graph[u])
{
ll v = k.first;
ll w = k.second;
if(!vis[v] && dist[v] > (dist[u] + w))
{
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
for(ll i = 0; i < n; i++)
shortest_path[src][i] = (dist[i] == 1E12) ? dist[i] : dist[i] - (H[src] - H[i]);
}
void print_all_distances()
{
for(ll i = 0; i < n; i++)
{
for(ll j = 0; j < n; j++)
{
if(shortest_path[i][j] == 1E12) cout << "#" << " ";
else cout << shortest_path[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main()
{
ll t; cin >> t;
for(ll idx = 1; idx <= t; idx++)
{
cin >> n >> m;
clear_everything();
for(ll i = 0; i < m; i++)
{
ll u, v, w; cin >> u >> v >> w;
u--; v--;
graph[u].pb({v, w});
edges.pb({{u, v}, w});
}
for(ll i = 0; i < n; i++)
{
graph[n].pb({i, (ll)0});
edges.pb({{n, i}, (ll)0});
}
if(bellman_ford(n)) // if the graph has negative weight cycles.
{
cout << "graph " << idx << " no" << endl << endl;
continue;
}
cout << "graph " << idx << " yes" << endl << endl;
update_weight_values();
for(ll i = 0; i < n; i++)
dijkstra(i);
print_all_distances();
}
return 0;
}