-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirected_weighted_graph.cpp
More file actions
45 lines (42 loc) · 918 Bytes
/
directed_weighted_graph.cpp
File metadata and controls
45 lines (42 loc) · 918 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void addEdge(vector<pair<int,int> > G[],int u,int v,int wt)
{
G[u].push_back(make_pair(v,wt));
G[v].push_back(make_pair(u,wt));
}
void print(vector<pair<int,int> > G[],int vertex)
{
int v,w;
for(int u=0;u<vertex;u++)
{
cout<<"\nNode "<<u<<" makes an edge with\n";
for(auto it=G[u].begin();it!=G[u].end();it++)
{
v=it->first;
w=it->second;
cout<<"\tNode "<<v<<" with edge weight "<<w<<"\n";
}
cout<<"\n";
}
}
int main(){
int vertex,u,v,wt,e;
cout<<"\nEnter vertex of the graph:\n";
cin>>vertex;
std::vector<pair<int,int> > G[vertex];
cout<<"\nEnter no. of edges:\n";
cin>>e;
for(int i=0;i<e;i++)
{
cout<<"\nEnter the vertex:\n";
cout<<"\nFrom:\n";
cin>>u;
cout<<"\nTo:\n";
cin>>v;
cout<<"\nEnter weight of the edge:\n";
cin>>wt;
addEdge(G,u,v,wt);
}
print(G,vertex);
}