Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One if condition is missing from Prims Efficient solution #16

Open
amantayal6797 opened this issue Jul 8, 2021 · 1 comment
Open

One if condition is missing from Prims Efficient solution #16

amantayal6797 opened this issue Jul 8, 2021 · 1 comment

Comments

@amantayal6797
Copy link

The given solution is breaking for the following test case on gfg
10 11
0 1 7
0 4 4
0 7 8
0 9 3
1 7 7
1 8 3
2 5 8
2 7 10
3 4 3
4 6 10
5 7 8

@amantayal6797
Copy link
Author

This is the correct code for the gfg problem

int spanningTree(int V, vector<vector> adj[]){
int res=0;
vector MST(V,false);
vector dist(V,INT_MAX);
dist[0]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
q.push({0,0});

while(q.size()!=0){
int ver;
ver=q.top().second;
q.pop();
if(MST[ver]==true) //This is very important
continue;

MST[ver]=true;
res+=dist[ver];

for(int j=0;j<adj[ver].size();j++){
int v=adj[ver][j][0];
int wt=adj[ver][j][1];
if(MST[v]==false && dist[v]>wt){
dist[v] = wt;
q.push({dist[v],v});
}
}
}
return res;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant