-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi_dijkstra.cpp
170 lines (160 loc) · 4.89 KB
/
bi_dijkstra.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
#include <bits/stdc++.h>
#include <algorithm>
#define INDEX 1 //set this to 1 if graph input starts with 1 as first node.
//#define UNDIRECTED // define this to process the input for undirected graph test case
using namespace std;
/*
* Bi-directional Dijkstra Algorithm
* Input format is based on Course Graph Assignment Week_6
*/
typedef enum
{
FORWARD, REVERSE
} direction;
#define MAX 2
typedef unsigned long long ull;
class Compare
{
public:
bool operator()(pair<int, int> a, pair<int, int> b)
{
return a.second > b.second;
}
};
class BiDijkstra
{
private:
int n; // nodes
int m; // edges
vector<double> dist[MAX]; //Distance of each node from s,t
vector<int> prev[MAX]; //Path trace
set<int> proc[MAX]; // Processed set
priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq[MAX]; // Priority Q
vector<vector<int> > G[MAX]; // Adj List
vector<vector<int> > cost[MAX]; // Cost
vector<bool> visited[MAX];
public:
BiDijkstra(int n, int m)
{
this->n = n;
this->m = m;
//Adjacency List
G[FORWARD].assign(n, vector<int>());
G[REVERSE].assign(n, vector<int>());
//Adjacency List
cost[FORWARD].assign(n, vector<int>());
cost[REVERSE].assign(n, vector<int>());
dist[FORWARD].assign(n, INFINITY);
dist[REVERSE].assign(n, INFINITY);
prev[FORWARD].assign(n, -1);
prev[REVERSE].assign(n, -1);
visited[FORWARD].assign(n, 0);
visited[REVERSE].assign(n, 0);
}
void Reset()
{
pq[FORWARD] = priority_queue<pair<int, int>, vector<pair<int, int>>,
Compare>(); // reset it
pq[REVERSE] = priority_queue<pair<int, int>, vector<pair<int, int>>,
Compare>(); // reset it
proc[FORWARD].clear();
proc[REVERSE].clear();
dist[FORWARD].assign(n, INFINITY);
dist[REVERSE].assign(n, INFINITY);
visited[FORWARD].assign(n, 0);
visited[REVERSE].assign(n, 0);
}
void AddNode(int s, int t, int w)
{
G[FORWARD][s- INDEX].push_back(t- INDEX);
G[REVERSE][t- INDEX].push_back(s- INDEX);
cost[FORWARD][s- INDEX].push_back(w);
cost[REVERSE][t- INDEX].push_back(w);
#ifdef UNDIRECTED
G[FORWARD][t- INDEX].push_back(s- INDEX);
G[REVERSE][s- INDEX].push_back(t- INDEX);
cost[FORWARD][t- INDEX].push_back(w);
cost[REVERSE][s- INDEX].push_back(w);
#endif
}
void Process(int node, direction d)
{
for (int i = 0; i < G[d][node].size(); i++)
{
if (dist[d][G[d][node][i]] > (dist[d][node] + cost[d][node][i])) //Relax
{
dist[d][G[d][node][i]] = (dist[d][node] + cost[d][node][i]);
pq[d].push(make_pair(G[d][node][i], dist[d][G[d][node][i]]));
}
}
proc[d].insert(node);
}
int ShortestPath()
{
//Logic for set union
vector<int> v(proc[FORWARD].size() + proc[REVERSE].size());
vector<int>::iterator it;
it = set_union(proc[FORWARD].begin(), proc[FORWARD].end(),
proc[REVERSE].begin(), proc[REVERSE].end(), v.begin());
v.resize(it - v.begin());
double distance = INFINITY;
int u_best = -1;
for (auto &i : v)
{
if ((dist[FORWARD][i] + dist[REVERSE][i]) < distance)
{
u_best = i;
distance = (dist[FORWARD][i] + dist[REVERSE][i]);
}
}
return distance;
}
int FindPath(int s, int t)
{
dist[FORWARD][s] = 0;
dist[REVERSE][t] = 0;
pq[FORWARD].push(make_pair(s, 0));
pq[REVERSE].push(make_pair(t, 0));
while (pq[FORWARD].size() && pq[REVERSE].size())
{
for (int i = FORWARD; i < MAX; i++)
{
direction d = static_cast<direction>(i);
//ExtractMin
int n = pq[i].top().first;
pq[i].pop();
if (visited[d][n])
continue;
Process(n, d);
visited[d][n] = true;
bool other = (d == FORWARD) ? 1 : 0;
if (proc[other].find(n) != proc[other].end()) // Found in other-set
return ShortestPath();
}
}
return -1;
}
};
int main()
{
int n, m;
std::cin >> n >> m;
BiDijkstra b(n, m);
for (int i = 0; i < m; i++)
{
int x, y, w;
cin >> x >> y >> w;
b.AddNode(x, y, w);
}
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int s, t;
cin >> s >> t;
s = s - INDEX;
t = t - INDEX;
cout << b.FindPath(s, t) << "\n";
b.Reset();
}
}