-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAP11vec.cpp
More file actions
224 lines (192 loc) · 4.81 KB
/
AP11vec.cpp
File metadata and controls
224 lines (192 loc) · 4.81 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <vector>
#include <iterator>
#include <queue>
#include <stack>
#include <limits>
#define endl "\n"
#define UNVISITED 0
#define VISITED 1
using namespace std;
typedef struct g{
vector<pair<int, int>> *l; // adjacency list
int numEdge; // number of edges
int n; // number of vertices
int *Mark; // auxiliary marking array
int *D;
} G;
G* create_graph(const int n);
int n(G* g);
int e(G* g);
int first(G* g, int v);
int next(G* g, int v, vector<pair<int, int>>::iterator &it);
int weight(G* g, vector<pair<int, int>>::iterator it);
void graphTraverse(G* g, int v);
void DFS(G* g, int v);
void BFS(G* g, int start);
void Dijkstra(G* g, int s);
void setEdge(G* g, int i, int j, int w);
void delEdge(G* g, int i, int j);
bool isEdge(G* g, int i, int j);
void setMark(G* g, int v, int val);
int getMark(G* g, int v);
void toposort(G* g, int v, stack<int> &s);
void printStack(stack<int> s);
int main(void) {
int n, m, v;
cin >> n >> m >> v;
G* g = create_graph(n);
for(int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
setEdge(g, x, y, w);
}
graphTraverse(g, v);
for(int i = 0; i < n; i++) {
cout << g->D[i] << " ";
}
cout << endl;
return 0;
}
G* create_graph(int n) {
G* g = new G;
g->n = n;
g->Mark = new int[n];
g->D = new int[n];
g->l = new vector<pair<int, int>>[n];
g->numEdge = 0;
return g;
}
int n(G* g) {
return g->n;
}
int e(G* g) {
return g->numEdge;
}
int first(G* g, int v) {
if(g->l[v].empty()) {
return n(g);
}
return g->l[v].front().first;
}
int next(G* g, int v, vector<pair<int, int>>::iterator &it) {
it++;
if(it == g->l[v].end()) {
return n(g);
}
else {
return (*it).first;
}
}
int weight(G* g, vector<pair<int, int>>::iterator it) {
return (*it).second;
}
void graphTraverse(G* g, int v) {
for(int i = 0; i <= (n(g)-1); i++) {
setMark(g, i, UNVISITED);
}
if(getMark(g, v) == UNVISITED) {
Dijkstra(g, v);
}
}
void DFS(G* g, int v) {
//preVisit(g, v); do something before visiting the node
setMark(g, v, VISITED);
int w = first(g, v);
while(w < n(g)) {
if(getMark(g, w) == UNVISITED) {
DFS(g, w);
}
//w = next(g, v, w);
}
//posVisit(g, v); do something after visiting the vertex
}
void BFS(G* g, int start) {
queue<int> Q;
Q.push(start);
setMark(g, start, VISITED);
while(Q.size() > 0) {
int v = Q.front();
Q.pop();
//preVisit(g, v); do something before visiting the vertex
int w = first(g, v);
while(w < n(g)) {
if(getMark(g, w) == UNVISITED) {
setMark(g, w, VISITED);
Q.push(w);
}
//w = next(g, v, w);
}
//posVisit(g, v); do something after visiting the vertex
}
}
void Dijkstra(G* g, int s) {
int *P = new int[g->n];
int p, v;
vector<pair<int, int>>::iterator it;
priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>>> H;
for(int i = 0; i <= (n(g)-1); i++) {
g->D[i] = numeric_limits<int>::max();
P[i] = -1;
}
H.push(make_pair(0, make_pair(s, s)));
g->D[s] = 0;
for(int i = 0; i <= (n(g)-1); i++) {
do {
if(H.empty()) {
return;
}
pair<int, pair<int,int>> tmp = H.top();
H.pop();
p = tmp.second.first;
v = tmp.second.second;
} while(!(g->Mark[v] == UNVISITED));
setMark(g, v, VISITED);
P[v] = p;
int w = first(g, v);
it = g->l[v].begin();
while(w < n(g)) {
if(getMark(g, w) != VISITED && g->D[w] > g->D[v] + weight(g, it)) {
g->D[w] = g->D[v] + weight(g, it);
H.push(make_pair(g->D[w], make_pair(v, w)));
}
w = next(g, v, it);
}
}
}
void setEdge(G* g, int i, int j, int w) {
g->numEdge++;
g->l[i].push_back(make_pair(j, w));
g->l[j].push_back(make_pair(i, w)); // only for undirected graphs
}
void delEdge(G* g, int i, int j) {
g->numEdge--;
//int w = weight(g, i, j);
//g->l[i].remove(make_pair(j, w));
}
bool isEdge(G* g, int i, int j) {
return true;
}
void setMark(G* g, int v, int val) {
g->Mark[v] = val;
}
int getMark(G* g, int v) {
return g->Mark[v];
}
void toposort(G* g, int v, stack<int> &s) {
setMark(g, v, VISITED);
int w = first(g, v);
while(w < n(g)) {
if(getMark(g, w) == UNVISITED) {
toposort(g, w, s);
}
//w = next(g, v, w);
}
s.push(v);
}
void printStack(stack<int> s) {
while(!(s.empty())) {
cout << s.top() << " ";
s.pop();
}
}