fork(19) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5.   389
  6.   0 --------- 1
  7.   | |
  8.   | |
  9.   405 | | 818
  10.   | |
  11.   | |
  12.   2 --------- 3
  13.   765
  14.  
  15. */
  16.  
  17.  
  18. const int N = 4;
  19. vector<pair<int, int> > adj[N];
  20.  
  21.  
  22. int main(){
  23. // The following two lines are for a faster io with cin and cout
  24. ios_base::sync_with_stdio(false);
  25. cin.tie(NULL);
  26.  
  27.  
  28. // build the graph
  29. adj[0].push_back(make_pair(1, 389));
  30. adj[0].push_back(make_pair(2, 405));
  31.  
  32. adj[1].push_back(make_pair(0, 389));
  33. adj[1].push_back(make_pair(3, 818));
  34.  
  35. adj[2].push_back(make_pair(0, 405));
  36. adj[2].push_back(make_pair(3, 765));
  37.  
  38. adj[3].push_back(make_pair(1, 818));
  39. adj[3].push_back(make_pair(2, 765));
  40.  
  41.  
  42. // print the graph
  43. int v, w;
  44.  
  45. for (int u=0; u<N; u++) {
  46. cout << "Node u=" << u << " has the following neighbors:\n";
  47.  
  48. for (auto it=adj[u].begin(); it!=adj[u].end(); it++) {
  49. v = it->first;
  50. w = it->second;
  51. cout << "\tNode v=" << v << " with edge weight w=" << w << "\n";
  52. }
  53.  
  54. cout << "\n";
  55. }
  56.  
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Node u=0 has the following neighbors:
	Node v=1 with edge weight w=389
	Node v=2 with edge weight w=405

Node u=1 has the following neighbors:
	Node v=0 with edge weight w=389
	Node v=3 with edge weight w=818

Node u=2 has the following neighbors:
	Node v=0 with edge weight w=405
	Node v=3 with edge weight w=765

Node u=3 has the following neighbors:
	Node v=1 with edge weight w=818
	Node v=2 with edge weight w=765