Advertisement
Guest User

dijkstra.cpp

a guest
Mar 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. /* freopen("input.txt","r",stdin);
  8. freopen("output.txt","w",stdout);
  9. */
  10. int n ,archi;
  11. cin >> n >> archi ;
  12. int inizio , fine;
  13. cin >> inizio >> fine;
  14.  
  15.  
  16. vector<pair<int,int>> v[n];
  17.  
  18.  
  19.  
  20. int a,b,c;
  21. for(int i =0;i< archi;i++){
  22. cin >> a >> b >> c;
  23. a--;
  24. b--;
  25. v[a].push_back({b,c});
  26. v[b].push_back({a,c});
  27.  
  28. }
  29.  
  30. ;
  31.  
  32. vector<int> dist(n,2e9);
  33.  
  34.  
  35. queue<int> coda;
  36.  
  37.  
  38. coda.push(inizio);
  39. dist[inizio]=0;
  40.  
  41. while(!coda.empty()){
  42.  
  43. int ii=coda.front();
  44. coda.pop();
  45.  
  46.  
  47. for(auto x : v[ii]){
  48.  
  49. if(dist[x.first] > dist[ii] + x.second){
  50. coda.push(x.first);
  51.  
  52. dist[x.first] = dist[ii] + x.second;
  53. }
  54.  
  55.  
  56.  
  57. }
  58. }
  59.  
  60. cout << dist[fine];
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement