You are viewing a single comment's thread. Return to all comments →
easy c++ solution using priority_queue:
vector bfs(int n, int m, vector> edges, int s) {
int dist[n]; map<int,vector<int>>mp; for(auto x:edges) { mp[x[0]].push_back(x[1]); mp[x[1]].push_back(x[0]); } for (int i=1;i<=n;i++){ dist[i] = INT_MAX; } priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; pq.push({0,s}); dist[s] = 0; while(!pq.empty()) { int cost = pq.top().first; int src = pq.top().second; pq.pop(); for(auto x: mp[src]) { if(dist[x] > cost +6) { dist[x] = cost+6; pq.push({dist[x],x}); } } } vector<int>ans; for(int i=1;i<=n;i++) { if(i==s) { continue; } else if(dist[i] == INT_MAX) { ans.push_back(-1); } else { ans.push_back(dist[i]); } } return ans;
} } } return ans; }
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
easy c++ solution using priority_queue:
vector bfs(int n, int m, vector> edges, int s) {
} } } return ans; }