Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- struct edge
- {
- int u, v;
- int weight;
- // sort by weight and here we overload < operator.
- bool operator < (const edge &p) const {
- return weight < p.weight;
- }
- };
- vector <edge> e;
- int main()
- {
- int nodes, edges;
- cin >> nodes >> edges;
- for(int i = 0; i < edges; i++)
- {
- edge get;
- int u, v, weight;
- cin >> u >> v >> weight;
- get.u = u;
- get.v = v;
- get.weight = weight;
- e.push_back(get);
- }
- sort(e.begin(), e.end());
- for(int i = 0; i < edges; i++)
- {
- // to access the value of structure list
- // we can use vector_name[counter].member_name;
- cout <<"first_node: " << e[i].u << " second_node : "<< e[i].v;
- cout << " cost : " << e[i].weight << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement