-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Representation3.cpp
81 lines (71 loc) · 1.5 KB
/
Graph_Representation3.cpp
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
/********************************************
* GRAPH REPRESENTATIONS using STL
* @author Amirul Islam (shiningflash)
* *
* The program basically prints adjacency list
* of an undirected and weighted graph
*********************************************/
#include <bits/stdc++.h>
using namespace std;
void printGraph(vector < pair <int, int> > graph[], int numberfVertex) {
for (int u = 0; u < numberfVertex; u++) {
cout << "Node " << u << "\n-\n";
for (auto it = graph[u].begin(); it != graph[u].end(); it++) {
int v = it->first;
int w = it->second;
cout << "( " << u << " - " << v << " ) - " << w << "\n";
}
cout << "\n";
}
}
int main() {
int numberOfVertex, numberOfEdge, u, v, w;
cin >> numberOfVertex >> numberOfEdge;
vector < pair <int, int> > graph[numberOfVertex];
for (int i = 0; i < numberOfEdge; i++) {
cin >> u >> v >> w;
graph[u].push_back(make_pair(v, w));
graph[v].push_back(make_pair(u, w));
}
printGraph(graph, numberOfVertex);
}
/******************
INPUT
---
- 1st line, here, 5 nVertex, 7 nEdge
- after then, u - v - weight
5 7
0 1 10
0 4 20
1 2 30
1 3 40
1 4 50
2 3 60
3 4 70
OUTPUT:
-----
Node 0
-
( 0 - 1 ) - 10
( 0 - 4 ) - 20
Node 1
-
( 1 - 0 ) - 10
( 1 - 2 ) - 30
( 1 - 3 ) - 40
( 1 - 4 ) - 50
Node 2
-
( 2 - 1 ) - 30
( 2 - 3 ) - 60
Node 3
-
( 3 - 1 ) - 40
( 3 - 2 ) - 60
( 3 - 4 ) - 70
Node 4
-
( 4 - 0 ) - 20
( 4 - 1 ) - 50
( 4 - 3 ) - 70
*****************/