why is it showing run time error
Dijkstra algorithm
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ll long long int map<int,list<pair<int,int> > > m; void addedge(int u,int v,int w){ m[u].pb(mp(v,w)); m[v].pb(mp(u,w)); } void print(){ for(auto i:m){ cout<<i.first<<"->"; for(auto j:m[i.first]){ cout<<"("<<j.first<<","<<j.second<<")"; }cout<<endl; } } void dijkstra(int src,int n){ map<ll,int> dist; bool visited[n]; for(int i=1;i<=n;i++){ visited[i]=false; } for(auto i:m){ dist[i.first]=INT_MAX; } dist[src]=0; set<pair<int,int> > s; s.insert(mp(0,src)); while(!s.empty()){ auto f=*(s.begin()); ll dist1 = f.first; s.erase(s.begin()); int node =f.second; visited[node]=true; for(auto c:m[f.second]){ if(dist1 + c.second<dist[c.first]){ int dest= c.first; auto p = s.find(mp(dist[dest],dest)); if(p!=s.end()){ s.erase§; } dist[dest]= dist1 + c.second; s.insert(mp(dist[dest],dest)); } } } for(int i=1;i<=n;i++){ if(i!=src){ if(visited[i]==true){ cout<<dist[i]<<" “; } else{ cout<<”-1"<<" "; } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t–){ int n,e; cin>>n>>e; while(e–){ int u,v,w; cin>>u>>v>>w; addedge(u,v,w); } int y; cin>>y; dijkstra(y,n); cout<<endl; } }
please upload the code to cb.lk/ide and share the link
Hi Shashank, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.