In Set 1, unweighted graph is discussed. In this post, weighted graph representation using STL is discussed. The implementation is for adjacency list representation of weighted graph.
We use two STL containers to represent graph:
- vector : A sequence container. Here we use it to store adjacency lists of all vertices. We use vertex number as index in this vector.
- pair : A simple container to store pair of elements. Here we use it to store adjacent vertex number and weight of edge connecting to the adjacent.
The idea is to use a vector of pair vectors. Below code implements the same.
Node 0 makes an edge with Node 1 with edge weight =10 Node 4 with edge weight =20 Node 1 makes an edge with Node 0 with edge weight =10 Node 2 with edge weight =30 Node 3 with edge weight =40 Node 4 with edge weight =50 Node 2 makes an edge with Node 1 with edge weight =30 Node 3 with edge weight =60 Node 3 makes an edge with Node 1 with edge weight =40 Node 2 with edge weight =60 Node 4 with edge weight =70 Node 4 makes an edge with Node 0 with edge weight =20 Node 1 with edge weight =50 Node 3 with edge weight =70
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Prefix Sum Array - Implementation and Applications in Competitive Programming
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Detect cycle in the graph using degrees of nodes of graph
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Longest path in a directed Acyclic graph | Dynamic Programming
- Convert undirected connected graph to strongly connected directed graph
- How can competitive programming help you get a job?
- Bit Tricks for Competitive Programming
- How to become a master in competitive programming?
- What is Competitive Programming and How to Prepare for It?
- How to begin with Competitive Programming?
- Fast I/O for Competitive Programming
- A Better Way To Approach Competitive Programming
Improved By : Ritesh Ghorse