Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
10
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;
}
Editor is loading...