Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- struct edge
- {
- int idx;
- int a;
- int b;
- int cost;
- };
- struct vertex
- {
- bool visited = false;
- std::vector<struct edge> adj_list = {};
- int count_edges = 0;
- };
- void addEdge(int i, int j, int cost, vector<vertex>& vertexes)
- {
- edge buff;
- buff.idx = j;
- buff.cost = cost;
- buff.a = i;
- buff.b = j;
- vertexes[i].adj_list.push_back(buff);
- buff.idx = i;
- vertexes[j].adj_list.push_back(buff);
- vertexes[i].count_edges++;
- vertexes[j].count_edges++;
- }
- struct Compare
- {
- bool operator()(edge& o1, edge& o2) const
- {
- return o1.cost > o2.cost;
- }
- };
- void showpq(priority_queue<struct edge, vector<struct edge>, Compare> gq)
- {
- priority_queue<struct edge, vector<struct edge>, Compare> g = gq;
- while (!g.empty())
- {
- cout << '\t' << g.top().cost;
- g.pop();
- }
- cout << '\n';
- }
- int main()
- {
- priority_queue<struct edge, vector<struct edge>, Compare> heap;
- int n, m;
- cin >> n >> m;
- int a, b, c;
- vector<struct vertex> vertexes(n);
- struct edge EDG2;
- for (int i = 0; i < m; ++i)
- {
- cin >> a >> b >> c;
- addEdge(a - 1, b - 1, c, vertexes);
- if (a == 1 | b == 1)
- {
- EDG2.a = a - 1;
- EDG2.b = b - 1;
- EDG2.cost = c;
- heap.push(EDG2);
- }
- }
- showpq(heap);
- cout << "\n print:" << endl;
- for (int i = 0; i < n; i++)
- {
- int size = vertexes[i].adj_list.size();
- cout << "idx =: ";
- for (int j = 0; j < size; ++j)
- {
- cout << vertexes[i].adj_list[j].a << " " << vertexes[i].adj_list[j].b << " ;";
- }
- cout << "; edge_value =: ";
- for (int j = 0; j < size; ++j)
- {
- cout << vertexes[i].adj_list[j].cost << " ";
- }
- cout << endl;
- }
- vertexes[0].visited = true;
- struct edge EDG;
- int new_vertex;
- int max = 0;
- for (int i = 0; i < n - 1; ++i)
- {
- EDG = heap.top();
- heap.pop();
- cout << "Взял очередное мин ребро " << EDG.a + 1 << " " << EDG.b + 1 << " " << EDG.cost
- << endl;
- if (max < EDG.cost)
- {
- max = EDG.cost;
- cout << "new max = " << max << endl;
- }
- if (vertexes[EDG.a].visited)
- {
- new_vertex = EDG.b;
- cout << EDG.a + 1 << " уже посещено поэтому новая вершина - " << EDG.b + 1 << endl;
- vertexes[new_vertex].visited = true;
- }
- else
- {
- new_vertex = EDG.a;
- cout << EDG.b + 1 << " уже посещено поэтому новая вершина - " << EDG.a + 1 << endl;
- vertexes[new_vertex].visited = true;
- }
- cout << "new vert = " << new_vertex + 1 << endl;
- cout << " у new vert " << vertexes[new_vertex].count_edges
- << " рёбер, щас буду их добавлять " << endl;
- for (int j = 0; j < vertexes[new_vertex].count_edges; ++j)
- {
- EDG = vertexes[new_vertex].adj_list[j];
- cout << "Добавляю ребро " << EDG.a + 1 << " " << EDG.b + 1 << " " << EDG.cost
- << " ... ";
- if (not vertexes[EDG.idx].visited)
- {
- // EDG.a = new_vertex;
- // EDG.b = EDG.idx;
- heap.push(EDG);
- cout << "ребро с концом в не посещенной вершиной - добавлено" << endl;
- showpq(heap);
- }
- else
- cout << "ребро не подходит " << endl;
- }
- cout << " Закончил, на данный момент max = " << max << endl;
- }
- cout << "max = " << max << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment