Advertisement
Guest User

kola

a guest
Sep 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #include <list>
  4.  
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. int vertices, edges, v1, v2, weight;
  14.  
  15. printf("Enter the Number of Vertices -\n");
  16.  
  17. scanf("%d", &vertices);
  18.  
  19. printf("Enter the Number of Edges -\n");
  20.  
  21. scanf("%d", &edges);
  22.  
  23. // Adjacency List is a vector of list.
  24.  
  25. // Where each element is a pair<int, int>
  26.  
  27. // pair.first -> the edge's destination
  28.  
  29. // pair.second -> edge's weight
  30.  
  31. vector< list< pair<int, int> > > adjacencyList(vertices + 1);
  32.  
  33. printf("Enter the Edges V1 -> V2, of weight W\n");
  34.  
  35. for (int i = 1; i <= edges; ++i)
  36. {
  37.  
  38. scanf("%d%d%d", &v1, &v2, &weight);
  39.  
  40. // Adding Edge to the Directed Graph
  41.  
  42. adjacencyList[v1].push_back(make_pair(v2, weight));
  43.  
  44. }
  45.  
  46. printf("\nThe Adjacency List-\n");
  47.  
  48. // Printing Adjacency List
  49.  
  50. for (int i = 1; i < adjacencyList.size(); ++i)
  51. {
  52.  
  53. printf("adjacencyList[%d] ", i);
  54.  
  55. list< pair<int, int> >::iterator itr = adjacencyList[i].begin();
  56.  
  57. while (itr != adjacencyList[i].end())
  58. {
  59.  
  60. printf(" -> %d(%d)", (*itr).first, (*itr).second);
  61.  
  62. ++itr;
  63.  
  64. }
  65.  
  66. printf("\n");
  67.  
  68. }
  69.  
  70.  
  71.  
  72. return 0;
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement