Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. #define MAXN 50005
  4. #define INF 0x3f3f3f3f
  5. using namespace std;
  6. ifstream in("dijkstra.in");
  7. ofstream out("dijkstra.out");
  8. int n,m,dist[MAXN];
  9. struct edge
  10. {
  11. int nod,c;
  12. edge(int nod, int c):nod(nod),c(c) {}
  13. bool operator<(const edge &oth) const
  14. {
  15. return c>oth.c;
  16. }
  17. };
  18. vector<edge> v[MAXN];
  19. void djikstra(int start)
  20. {
  21. int u,d,q,c,alt;
  22. priority_queue<edge> pq;
  23. for(int i=1; i<=n; ++i)
  24. dist[i]=INF;
  25. dist[start]=0;
  26. pq.push(edge(start,0));
  27. while(!pq.empty())
  28. {
  29. u=pq.top().nod,c=pq.top().c;
  30. pq.pop();
  31. if(c==dist[u])
  32. for(edge i : v[u])
  33. {
  34. q=i.nod,d=i.c,alt=c+d;
  35. if(alt<dist[q])
  36. {
  37. dist[q]=alt;
  38. pq.push(edge(q,dist[q]));
  39. }
  40. }
  41. }
  42. }
  43. int main()
  44. {
  45. in>>n>>m;
  46. for(int i=1; i<=m; ++i)
  47. {
  48. int a,b,c;
  49. in>>a>>b>>c;
  50. v[a].push_back(edge(b,c));
  51. }
  52. djikstra(1);
  53. for(int i=2; i<=n; ++i)
  54. if(dist[i]==INF)out<<0<<' ';
  55. else out<<dist[i]<<' ';
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement