-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyListNode.cpp
More file actions
executable file
·48 lines (39 loc) · 1.13 KB
/
AdjacencyListNode.cpp
File metadata and controls
executable file
·48 lines (39 loc) · 1.13 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
#include <iostream>
#include <list>
#include <unordered_map>
#include <cstring>
#include <string>
using namespace std;
class Graph{
unordered_map<string, list<pair<string, int>>> l;
public:
void addEdge(string x, string y, bool bidir, int wt){
l[x].push_back(make_pair(y, wt));
if(bidir){
l[y].push_back(make_pair(x, wt));
}
}
void printAdjList(){
for(auto p: l){
string city = p.first;
list <pair <string, int>> nbrs = p.second;
cout << city << " -> ";
for(auto nbr : nbrs){
string dest = nbr.first;
int dist = nbr.second;
cout << dest << " " << dist << ",";
}
cout << endl;
}
}
};
int main(){
Graph g;
g.addEdge("A", "B", true, 20);
g.addEdge("B", "D", true, 40);
g.addEdge("A", "C", true, 10);
g.addEdge("C", "D", true, 40);
g.addEdge("A", "D", false, 50);
g.printAdjList();
return 0;
}