-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetro.cpp
More file actions
241 lines (152 loc) · 3.79 KB
/
metro.cpp
File metadata and controls
241 lines (152 loc) · 3.79 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
#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
// ANSI Colors
#define BLUE "\033[34m"
#define YELLOW "\033[33m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
class Graph{
public:
unordered_map<string, vector<pair<string,int>>> adj;
unordered_map<string,string> lineColor;
void addStation(string name,string color){
lineColor[name]=color;
}
void addEdge(string u,string v,int w){
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
void displayStations(){
cout<<"\n====== DELHI METRO STATIONS ======\n\n";
for(auto &x:adj){
string station=x.first;
string color=lineColor[station];
if(color=="BLUE")
cout<<BLUE<<station<<RESET<<endl;
else if(color=="YELLOW")
cout<<YELLOW<<station<<RESET<<endl;
else if(color=="RED")
cout<<RED<<station<<RESET<<endl;
else
cout<<station<<endl;
}
}
void shortestPath(string src,string dest){
unordered_map<string,int> dist;
unordered_map<string,string> parent;
for(auto &x:adj)
dist[x.first]=INT_MAX;
priority_queue<
pair<int,string>,
vector<pair<int,string>>,
greater<pair<int,string>>> pq;
dist[src]=0;
pq.push({0,src});
while(!pq.empty()){
auto node=pq.top();
pq.pop();
int cost=node.first;
string u=node.second;
for(auto &nbr:adj[u]){
string v=nbr.first;
int weight=nbr.second;
if(cost+weight < dist[v]){
dist[v]=cost+weight;
parent[v]=u;
pq.push({dist[v],v});
}
}
}
cout<<"\nShortest Distance: "<<dist[dest]<<" km\n";
vector<string> path;
string cur=dest;
while(cur!=src){
path.push_back(cur);
cur=parent[cur];
}
path.push_back(src);
cout<<"\nRoute:\n";
for(int i=path.size()-1;i>=0;i--){
string station=path[i];
string color=lineColor[station];
if(color=="BLUE")
cout<<BLUE<<station<<RESET;
else if(color=="YELLOW")
cout<<YELLOW<<station<<RESET;
else if(color=="RED")
cout<<RED<<station<<RESET;
else
cout<<station;
if(i!=0)
cout<<" -> ";
}
cout<<endl;
}
};
void createMetro(Graph &g){
// Blue line stations
g.addStation("Noida Sector 62","BLUE");
g.addStation("Botanical Garden","BLUE");
g.addStation("Yamuna Bank","BLUE");
g.addStation("Rajiv Chowk","BLUE");
g.addStation("Moti Nagar","BLUE");
g.addStation("Janakpuri West","BLUE");
g.addStation("Dwarka Sector 21","BLUE");
// Yellow line
g.addStation("AIIMS","YELLOW");
g.addStation("Saket","YELLOW");
g.addStation("Hauz Khas","YELLOW");
g.addStation("Chandni Chowk","YELLOW");
g.addStation("New Delhi","YELLOW");
// Red line
g.addStation("Kashmere Gate","RED");
g.addStation("Shastri Park","RED");
g.addStation("Seelampur","RED");
// Connections
g.addEdge("Noida Sector 62","Botanical Garden",8);
g.addEdge("Botanical Garden","Yamuna Bank",10);
g.addEdge("Yamuna Bank","Rajiv Chowk",6);
g.addEdge("Rajiv Chowk","Moti Nagar",9);
g.addEdge("Moti Nagar","Janakpuri West",7);
g.addEdge("Janakpuri West","Dwarka Sector 21",6);
g.addEdge("Rajiv Chowk","New Delhi",1);
g.addEdge("New Delhi","Chandni Chowk",2);
g.addEdge("Rajiv Chowk","AIIMS",7);
g.addEdge("AIIMS","Hauz Khas",3);
g.addEdge("Hauz Khas","Saket",2);
g.addEdge("Chandni Chowk","Kashmere Gate",2);
g.addEdge("Kashmere Gate","Shastri Park",3);
g.addEdge("Shastri Park","Seelampur",2);
}
int main(){
Graph g;
createMetro(g);
int choice;
while(true){
cout<<"\n===== DELHI METRO APP =====\n";
cout<<"1 Show Stations\n";
cout<<"2 Shortest Route\n";
cout<<"3 Exit\n";
cin>>choice;
if(choice==1){
g.displayStations();
}
else if(choice==2){
string src,dest;
cin.ignore();
cout<<"Enter Source: ";
getline(cin,src);
cout<<"Enter Destination: ";
getline(cin,dest);
g.shortestPath(src,dest);
}
else if(choice==3){
break;
}
}
}