-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFire Station Problem
More file actions
371 lines (250 loc) · 5.44 KB
/
Fire Station Problem
File metadata and controls
371 lines (250 loc) · 5.44 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Question
A city is served by a number of fire stations.
Residents have complained that the distance between certain houses and the nearest station is too far,
so a new station is to be built. You are to choose the location of the new station so as to reduce the distance
to the nearest station from the houses of the poorest-served residents. The city has up to 500 intersections,
connected by road segments of various lengths. No more than 20 road segments intersect at a given intersection.
The locations of houses and fire stations alike are considered to be at intersections. Furthermore, we assume that
there is at least one house associated with every intersection. There may be more than one fire station per
intersection.
*/
// algo_FireStation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<limits.h>
using namespace std;
struct Lqueue
{
int items[500];
int front;
int rear;
};
void enqueue(Lqueue *l,int thing)
{
if(l->rear==499)
cout<<"The Queue is Full!";
else
{
l->rear++;
l->items[l->rear]=thing;
}
}
int dequeue(Lqueue *l) // the method for inserting an element into the queue
{
if(l->front>l->rear)
{
cout<<"The queue is empty!";
return NULL;
}
else
{
int x=l->items[l->front];
l->front++;
return x;
}
}
int FindMinDistance(int distance[6], bool visited[6])
{
int index=-1;
int min=90000;
for(int i=1;i<6;i++)
{
if(visited[i]==false)
{
if(distance[i]<min)
{
min=distance[i];
index=i;
}
}
}
return index;
}
int FindMinDistanceFS(int distance[6], bool visited[6], bool IsFireStationConstructed[6])
{
int index=-1;
int min=90000;
for(int i=1;i<6;i++)
{
if(visited[i]==false && IsFireStationConstructed[i]==false)
{
if(distance[i]<min)
{
min=distance[i];
index=i;
}
}
}
return index;
}
void MemCopy(int tempdistance[6],int distance[6])
{
for(int i=1;i<6;i++)
tempdistance[i]=distance[i];
}
int FindMaxAndReturnIndex(int tempdistance[6], bool IsFireStationConstructed[6])
{
int max=-1;
int index=-1;
for(int i=1;i<6;i++)
{
if(IsFireStationConstructed[i]==false)
{
if(tempdistance[i]>max)
{
max=tempdistance[i];
index=i;
}
}
}
return index;
}
int _tmain(int argc, _TCHAR* argv[])
{
Lqueue q;
q.front=0;
q.rear=-1;
bool IsFireStationConstructed[6];
for(int w=1;w<6;w++)
IsFireStationConstructed[w]=false;
int E[6][6];
for(int i=1;i<6;i++)
for(int j=1;j<6;j++)
E[i][j]=1234567;
// Hard Coded adjacency matrix, just for checking the solution
E[1][2]=10;
E[2][1]=10;
E[2][3]=10;
E[2][4]=10;
E[2][5]=10;
E[3][2]=10;
E[4][2]=10;
E[5][2]=10;
int stations[]={1,2};
// The Fire Station is at vertex 1.
// Array that tells us at what distance is the nearest fire station for a given vertex.
int distance[6];
for(int h=1;h<6;h++)
{
distance[h]=1234567;
}
for(int u=0;u<2;u++)
{
bool visited[6];
int index;
for(int k=1;k<6;k++)
{
visited[k]=false;
}
for(int r=0;r<u;r++)
visited[stations[r]]=true;
// Since the fire station is at node
int c=stations[u];
distance[c]=0;
visited[c]=true;
//update distance matrix
for(int i=1;i<=5;i++)
{
if(visited[i]==false)
{
if(E[c][i]<distance[i])
{
distance[i]=E[c][i];
}
}
}
// Main loop of Dijkstra's starts here
for(int i=1;i<=5;i++)
{
if(visited[i]==false)
{
index=FindMinDistance(distance,visited);
// mark that node as visited
visited[index]=true;
for(int j=1;j<6;j++)
{
if(visited[j]==false)
{
if(distance[j]>distance[index]+E[index][j])
{
distance[j]=distance[index]+E[index][j];
}
}
}
}
}
}
for(int r=1;r<6;r++)
cout<<distance[r]<<"\t";
_getch();
// you have to now construct a fire station ( For simplicity, say one)
int max=1234567;
int RequiredIndex=-1;
int tempdistance[6];
bool tempvisited[6];
int tempindex;
int aindex;
for(int t=1;t<6;t++)
{
if(IsFireStationConstructed[t]==false)
{
MemCopy(tempdistance,distance);
for(int j=1;j<6;j++)
tempvisited[j]=false;
tempvisited[t]=true;
tempdistance[t]=0;
// update tempdistance matrix here..
for(int j=1;j<6;j++)
{
if(tempvisited[j]==false && IsFireStationConstructed[j]==false)
{
if(tempdistance[j]>tempdistance[t]+E[t][j])
{
tempdistance[j]=tempdistance[t]+E[t][j];
}
}
}
for(int i=1;i<6;i++)
{
if(IsFireStationConstructed[i]!=true)
{
if(tempvisited[i]==false)
{
tempindex=FindMinDistanceFS(tempdistance,tempvisited,IsFireStationConstructed);
// mark that node as visited
tempvisited[tempindex]=true;
// update tempdistance matrix
for(int j=1;j<6;j++)
{
if(tempvisited[j]==false && IsFireStationConstructed[j]==false)
{
if(tempdistance[j]>tempdistance[tempindex]+E[tempindex][j])
{
tempdistance[j]=tempdistance[tempindex]+E[tempindex][j];
}
}
}
}
}
}
for(int z=1;z<6;z++)
{
cout<<tempdistance[z]<<"\t";
}
_getch();
aindex=FindMaxAndReturnIndex(tempdistance,IsFireStationConstructed);
if(max>tempdistance[aindex])
{
RequiredIndex=aindex;
max=tempdistance[aindex];
}
_flushall();
}
}
cout<<"FireStation will be constructed at: "<<RequiredIndex;
return 0;
}