-
Notifications
You must be signed in to change notification settings - Fork 1
/
5A.cpp
283 lines (219 loc) · 3.5 KB
/
5A.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <bits/stdc++.h>
#include <time.h>
#include <unistd.h>
using namespace std;
struct edge
{
int to;
int wt;
};
struct node
{
int vertex;
int cost;
};
class HeapX
{
vector<node> V;
bool minn;
void swap(vector<node> &V, int a, int b)
{
// cout<<V[a].vertex<<" "<<V[b].vertex<<endl;
node temp = V[a];
V[a] = V[b];
V[b] = temp;
// cout<<V[a].vertex<<" "<<V[b].vertex<<endl;
}
void siftUp(int i)
{
if(i == 0)
return;
int par = i/2;
if((minn && V[i].cost < V[par].cost) || (!minn && V[i].cost > V[par].cost))
{
swap(V, i, par);
siftUp(par);
}
}
void siftDown(int i)
{
if(i == V.size())
return;
int optimal = i;
int lChild = 2*i;
int rChild = 2*i + 1;
if(lChild < V.size() && ((minn && V[lChild].cost < V[optimal].cost) || (!minn && V[lChild].cost > V[optimal].cost)))
optimal = lChild;
if(rChild < V.size() && ((minn && V[rChild].cost < V[optimal].cost) || (!minn && V[rChild].cost > V[optimal].cost)))
optimal = rChild;
if(optimal != i)
{
swap(V, i, optimal);
siftDown(optimal);
}
}
public:
void setMode(bool mode)
{
minn = mode;
}
void push(node Node)
{
V.push_back(Node);
siftUp(V.size()-1);
}
node top(void)
{
if(V.empty())
{
node temp;
temp.vertex = -1;
temp.cost = -1;
return temp;
}
return V[0];
}
void pop(void)
{
if(V.empty())
return;
int n = V.size();
V[0] = V[n-1];
V.pop_back();
siftDown(0);
}
void clear(void)
{
V.clear();
}
bool empty(void)
{
return V.empty();
}
};
class Graph
{
int N;
list<edge> *adj;
int t;
int S;
public:
Graph(int N)
{
this->N = N;
t = 0;
S = 0;
adj = new list<edge>[N];
}
void addEdge(int from, int to, int wt)
{
edge temp;
temp.to = to;
temp.wt = wt;
adj[from].push_back(temp);
}
void printGraph(void)
{
for(int i=0;i<N;i++)
{
cout<<(i)<<"->"<<endl;
list<edge>::iterator it;
for(it = adj[i].begin(); it != adj[i].end(); it++)
{
cout<<"("<<it->to<<", "<<it->wt<<")";
}
cout<<endl;
}
}
int dijkstraHeap(int dest)
{
vector<int> cost(N, 0);
for (int i=0; i<N; i++)
cost[i] = INT_MAX;
cost[0] = 0;
HeapX pq;
pq.setMode(1);
node source;
source.vertex = 0;
source.cost = 0;
pq.push(source);
while (!pq.empty())
{
node curr = pq.top();
pq.pop();
int u = curr.vertex;
if(u+1 == dest)
break;
list<edge>::iterator it;
for(it = adj[u].begin(); it != adj[u].end(); it++)
{
int v = it->to;
int l_uv = it->wt;
if(cost[v] == INT_MAX || cost[u] + l_uv < cost[v])
{
cost[v] = cost[u] + l_uv;
node temp;
temp.vertex = v;
temp.cost = cost[v];
pq.push(temp);
}
}
}
pq.clear();
return cost[dest-1];
}
};
int main()
{
int N = 200;
freopen("dijkstraData.txt", "r", stdin);
Graph G(N);
int x=0;
while(x < N)
{
string line;
getline(cin, line);
int f = 1;
string s = "";
string B = "", C = "";
for(int i=0; i<line.length(); i++)
{
if(line[i] == ' ' || line[i] == '\t')
{
if(f == 2)
{
C = s;
f = 1;
s = "";
G.addEdge(stoi(B) - 1, x, stoi(C));
G.addEdge(x, stoi(B) - 1, stoi(C));
}
else
{
s = "";
}
}
else if(line[i] == ',')
{
B = s;
f = 2;
s = "";
}
else
{
s += line[i];
}
}
x++;
}
// G.printGraph();
int destArr[10] = {7,37,59,82,99,115,133,165,188,197};
for(int i=0;i<10;i++)
{
int dest;
cin>>dest;
cout<<G.dijkstraHeap(destArr[i])<<" ";
}
cout<<endl;
return 0;
}