在线编译器中的分段C++没有打印消息

Segmentation C++ no print messages in online compiler

本文关键字:打印 消息 C++ 分段 编译器 在线      更新时间:2023-10-16

这是我当前的代码:

// PRIM MST fara heap lista adiacenta
#include<bits/stdc++.h> 
using namespace std; 
# define INF 0x3f3f3f3f 
// pereche int int denumita iPair
struct Edge {
int destination;
int weight;
};
Edge newEdge(int destination, int weight) {
return { destination, weight };
}
// To compare two points 
class edgeComparator
{ 
public: 
int operator() (const Edge& e1, const Edge& e2) 
{ 
printf("HIIIIIIIII222");
return e1.weight > e2.weight; 
} 
}; 
// un graf directionat cu reprezentare prin lista de adiacenta
class Graph 
{ 
int V; // Numar de noduri
// Lista care retine nodul si costul muchiei pentru fiecare pereche
list< Edge > *adj; 
public: 
Graph(int V) {
this->V = V;
adj = new list<Edge> [V]; 
}; // constructorul
// adauga o muchie grafului
void addEdge(int from, int to, int weight){
adj[from].push_back(newEdge(to, weight)); // http://www.cplusplus.com/reference/list/list/push_back/ pentru push back
adj[to].push_back(newEdge(from, weight)); 
};
// // Printeaza drumul cel mai scurt din sursa la toate celelalte noduri
void primMST(int numberElemOp){
// Creaza o coada de prioritati pentru a stoca toate nodurile.
// Sintaxa aparent o poti vedea aici http://geeksquiz.com/implement-min-heap-using-stl/ de ce e cum e
priority_queue< Edge, vector <Edge> , edgeComparator > pq; 
int src = 0; // Nodul 0 devine sursa
numberElemOp += 1;
// Creem un vector unde sa stocam cheile si le initializam cu infinit
vector<int> key(V, INF); 
// Pentru stocarea array-ului de parinti
vector<int> parent(V, -1); 
// Vector care ne arata ce noduri au fost incluse in mst
vector<bool> inMST(V, false); 
// Baga nodul parinte in coada de prioritati si face cheia 0
pq.push(newEdge(0, src));
key[src] = 0; 
numberElemOp += 2;
/* Pana cand coada devine vida */
while (!pq.empty()) 
{ 
int u = pq.top().destination; 
pq.pop(); 
inMST[u] = true; // Include nodul in MST
// // i este utilizat pentru a lua toate nodurile vecine
printf("HIIIIIIIII");
list< Edge >::iterator i;
printf("HIIIIIIIII");
numberElemOp += 4;
for (i = adj[u].begin(); i != adj[u].end(); ++i) 
{ 
// Ia numele si costul nodului vecin cu u
int v = (*i).destination; 
int weight = (*i).weight; 
// If v is not in MST and weight of (u,v) is smaller 
// than current key of v 
// Daca v nu este in MST si costul muchiei (u,v) este mai mic decat cheia lui v
numberElemOp += 5;
if (inMST[v] == false && key[v] > weight) 
{ 
// Updateaza cheia lui v
key[v] = weight; 
pq.push(newEdge(key[v], v)); 
parent[v] = u;
numberElemOp += 7;
} 
} 
} 
// Printeaza muchiile msg-ului
for (int i = 1; i < V; ++i) 
printf("%d - %dn", parent[i], i); 
printf("Numar operatii elementare: %d", numberElemOp);
}
}; 
int main() 
{ 
int numberElemOp;
// creaza un graf
printf("HIIIIIIIII");
int V = 9;
numberElemOp += 1;
Graph g(V); 
g.addEdge(0, 1, 4); 
g.addEdge(0, 7, 8); 
g.addEdge(1, 2, 8); 
g.addEdge(1, 7, 11); 
g.addEdge(2, 3, 7); 
g.addEdge(2, 8, 2); 
g.addEdge(2, 5, 4); 
g.addEdge(3, 4, 9); 
g.addEdge(3, 5, 14); 
g.addEdge(4, 5, 10); 
g.addEdge(5, 6, 2); 
g.addEdge(6, 7, 1); 
g.addEdge(6, 8, 6); 
g.addEdge(7, 8, 7); 
g.primMST(numberElemOp); 
return 0; 
} 

当我试着运行它时,我遇到了一个分段错误,我不太明白为什么。代码以这种形式工作:https://www.geeksforgeeks.org/prims-algorithm-using-priority_queue-stl/但现在已经不是了。

问题出在优先队列的某个位置,但我无法想象原因。。。

从队列中弹出元素后,访问inMST向量越界,为什么?

pq.push(newEdge(key[v], v)); // push new element into queue
^^^^    ^^

第一个arg应该是destination,但您正在传递distance,第二个arg应是weight,但您在传递destination。替换为:

pq.push(newEdge(v, key[v])); 

注释中已指出未初始化numberElemOp变量的问题。