Untitled
unknown
plain_text
2 years ago
1.3 kB
7
Indexable
#include <bits/stdc++.h>
using namespace std;
struct Edge {
char start;
char end;
int cost;
Edge(char s, char e, int c) : start(s), end(e), cost(c) {}
};
struct CompareEdges {
bool operator()(const Edge& e1, const Edge& e2) {
return e1.cost > e2.cost; // Min-heap based on cost
}
};
int main() {
int numVertices, numEdges;
cout << "Enter the number of vertices: ";
cin >> numVertices;
cout << "Enter the number of edges: ";
cin >> numEdges;
vector<Edge> edges;
priority_queue<Edge, vector<Edge>, CompareEdges> pq;
cout << "Enter edges (start end cost):" << endl;
for (int i = 0; i < numEdges; i++) {
char start, end;
int cost;
cin >> start >> end >> cost;
edges.push_back(Edge(start, end, cost));
}
for (const Edge& edge : edges) {
pq.push(edge);
}
cout << "Vertices in ascending order of cost:" << endl;
while (!pq.empty()) {
Edge minEdge = pq.top();
pq.pop();
cout << "Start: " << minEdge.start << " End: " << minEdge.end << " Cost: " << minEdge.cost << endl;
}
return 0;
}
/*
Enter the number of vertices: 5
Enter the number of edges: 8
Enter edges (start end cost):
A B 1
B D 2
D C 3
A C 4
A E 5
B E 3
D E 1
C E 2
*/Editor is loading...