Advertisement
Guest User

Untitled

a guest
May 28th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct edge
  8. {
  9. int u, v;
  10. int weight;
  11.  
  12. // sort by weight and here we overload < operator.
  13. bool operator < (const edge &p) const {
  14. return weight < p.weight;
  15. }
  16. };
  17.  
  18. vector <edge> e;
  19.  
  20. int main()
  21. {
  22. int nodes, edges;
  23.  
  24. cin >> nodes >> edges;
  25. for(int i = 0; i < edges; i++)
  26. {
  27. edge get;
  28. int u, v, weight;
  29.  
  30. cin >> u >> v >> weight;
  31.  
  32. get.u = u;
  33. get.v = v;
  34. get.weight = weight;
  35.  
  36. e.push_back(get);
  37. }
  38.  
  39. sort(e.begin(), e.end());
  40.  
  41. for(int i = 0; i < edges; i++)
  42. {
  43. // to access the value of structure list
  44. // we can use vector_name[counter].member_name;
  45. cout <<"first_node: " << e[i].u << " second_node : "<< e[i].v;
  46. cout << " cost : " << e[i].weight << endl;
  47. }
  48.  
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement