Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. const int N_MAX = 1000;
  8. const int inf = 2e28;
  9.  
  10. int main()
  11. {
  12. freopen("date.in", "r", stdin);
  13.  
  14. int n, m;
  15. vector<pair<int, int> > vecini[N_MAX];
  16. int distanta[N_MAX];
  17. int parinte[N_MAX];
  18.  
  19. scanf("%d %d", &n, &m);
  20.  
  21. for(int i = 0; i < m; i++){
  22. int x, y, cost;
  23. scanf("%d %d %d", &x, &y, &cost);
  24. x--;
  25. y--;
  26. vecini[x].push_back({cost, y});
  27. }
  28.  
  29. for(int i = 0; i < n; i++){
  30. distanta[i] = inf;
  31. parinte[i] = -1;
  32. }
  33. distanta[0] = 0;
  34.  
  35. for(int i = 1; i < n; i++){
  36. for(int j = 0; j < n; j++){
  37. for(auto p: vecini[j]){
  38. if(distanta[p.second] > distanta[j] + p.first){
  39. distanta[p.second] = distanta[j] + p.first;
  40. parinte[p.second] = j;
  41. }
  42. }
  43. }
  44. }
  45. bool cicluNegativ = false;
  46. for(int i = 0; i < n; i++){
  47. for(auto p: vecini[i]){
  48. if(distanta[p.second] > distanta[i] + p.first){
  49. cicluNegativ = true;
  50. }
  51. }
  52. }
  53.  
  54. if(cicluNegativ){
  55. printf("Ciclu negativ!!");
  56. }
  57. else{
  58. for(int i = 0; i < n; i++){
  59. if(distanta[i] == inf){
  60. printf("inf ");
  61. }
  62. else{
  63. printf("%d ", distanta[i]);
  64. }
  65. }
  66. }
  67.  
  68.  
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement