-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency_list_weighted.cpp
More file actions
57 lines (45 loc) · 1.17 KB
/
Adjacency_list_weighted.cpp
File metadata and controls
57 lines (45 loc) · 1.17 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
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
string name;
list<pair<string, int>> nbrs;
Node(string name){
this->name = name;
}
};
class Graph{
unordered_map<string, Node*> umap;
public:
Graph(vector<string> cities){
for(auto city: cities){
umap[city] = new Node(city);
}
}
void addEdge(string a, string b, int c, bool undir=false){
umap[a]->nbrs.push_back({b, c});
if (undir)
umap[b]->nbrs.push_back({a, c});
}
void printList(){
for(auto citypair: umap){
string city = citypair.first;
cout<<city<<"-->";
Node *nbr = citypair.second;
for(auto nbr: nbr->nbrs){
cout<<nbr.first<<" "<<nbr.second<<",";
//cout<<nbr.first<<",";
}
cout<<endl;
}
}
};
int main() {
vector<string> cities = {"delhi", "mumbai", "chennai", "kolkata"};
Graph g(cities);
g.addEdge("delhi", "mumbai", 3);
g.addEdge("delhi", "chennai", 2);
g.addEdge("chennai", "kolkata", 11);
g.addEdge("mumbai", "kolkata", 12);
g.printList();
}