Advertisement
Guest User

catalin e frae

a guest
Jan 29th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define oo 1e9
  3. using namespace std;
  4. ifstream fin("parc.in");
  5. ofstream fout("parc.out");
  6. int a[105][105], n, m, C, P, t[105], viz[105], d[105];
  7. void Citire()
  8. {
  9. int i, j, x, y, cost;
  10. fin >> n >> m >> C;
  11. for (i=1; i<n; i++)
  12. for (j=i+1; j<=n; j++)
  13. a[i][j]=a[j][i]=oo;
  14. for (i=1; i<=m; i++)
  15. {
  16. fin >> x >> y >> cost;
  17. a[x][y]=cost;
  18. a[y][x]=cost;
  19. }
  20. fin >> P;
  21. for (i=1; i<=P; i++)
  22. fin >> t[i];
  23. }
  24. void Dijkstra (int s)
  25. {
  26. int i, p, minim, pas;
  27. for (i=1; i<=n; i++)
  28. {
  29. d[i]=oo;
  30. viz[i]=0;
  31. }
  32. d[s]=0;
  33. for (pas=1; pas<n; pas++)
  34. {
  35. minim=oo;
  36. p=0;
  37. for (i=1; i<=n; i++)
  38. if (minim>d[i] and viz[i]==0)
  39. {
  40. minim=d[i];
  41. p=i;
  42. }
  43. if (p==0) return ;
  44. viz[p]=1;
  45. for (i=1; i<=n; i++)
  46. if (a[p][i]<oo and d[i]>d[p]+a[p][i])
  47. d[i]=d[p]+a[p][i];
  48. }
  49. }
  50. void Afisare()
  51. {
  52. int dmin=oo, i, nod=0;
  53. for (i=1; i<=P; i++)
  54. if (dmin>d[t[i]])
  55. {
  56. dmin=d[t[i]];
  57. nod=t[i];
  58. }
  59. else if (dmin==d[t[i]])
  60. nod = min (nod, t[i]);
  61. fout << nod;
  62. }
  63. int main()
  64. {
  65. Citire();
  66. Dijkstra(C);
  67. Afisare();
  68. fin.close();
  69. fout.close();
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement