YorKnEz

Untitled

Nov 18th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include<fstream>
  2. #include<vector>
  3. #include<queue>
  4. #define NM 100001
  5. #define INF 2000010
  6. #include<stdio.h>
  7.  
  8. using namespace std;
  9.  
  10. ifstream fin("dijkstra2.in");
  11. ofstream fout("dijkstra2.out");
  12.  
  13. int n, m, p, d[NM], inq[NM];
  14.  
  15. struct pereche {
  16. int y,c;
  17. };
  18.  
  19. struct compar {
  20. bool operator()(int x,int y) {
  21. return d[x]>d[y];
  22. }
  23. };
  24.  
  25. vector<pereche> G[NM];
  26.  
  27. priority_queue <int, vector<int>, compar> q;
  28.  
  29. void Citeste() {
  30. int i, x, y, c;
  31. pereche u;
  32. fin>>n>>m>>p;
  33. for (i=1; i<=m; ++i) {
  34. fin>>x>>y>>c;
  35. u.y=y;
  36. u.c=c;
  37. G[x].push_back(u);
  38. u.y=x;
  39. G[y].push_back(u);
  40. }
  41. }
  42.  
  43. void Dijkstra(int x0) {
  44. int z,lg,i;
  45. for (i=1; i<=n; i++) d[i]=INF;
  46. d[x0]=0;
  47. inq[x0]=1;
  48. q.push(x0);
  49. while (!q.empty()) {
  50. int j,im,y,c;
  51. im=q.top();
  52. q.pop();
  53. inq[im]=0;
  54. for(j=0; j<G[im].size(); j++) {
  55. y=G[im][j].y;
  56. c=G[im][j].c;
  57. lg=d[im]+c;
  58. if(lg<d[y]) {
  59. d[y]=lg;
  60. if(inq[y]==0) {
  61. inq[y]=1;
  62. q.push(y);
  63. }
  64. }
  65. }
  66. }
  67. }
  68.  
  69. void Scrie() {
  70. int i;
  71. for (i=1; i<=n; ++i)
  72. if(d[i]<INF)
  73. fout<<d[i]<<" ";
  74. else fout<<-1<<' ';
  75. }
  76. int main() {
  77. Citeste();
  78. Dijkstra(p);
  79. Scrie();
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment