Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5.  
  6. const int maxen = 4e18;
  7. const int Size = 1e5;
  8. signed main()
  9. {
  10.  
  11.     freopen("path.in", "r", stdin);
  12.     freopen("path.out", "w", stdout);
  13.  
  14.     int n, m, s;
  15.     cin >> n >> m >> s;
  16.     s--;
  17.     vector <pair <int, pair<int, int>>> edges(Size);
  18.     vector <int> d(Size, maxen);
  19.     vector <int> path;
  20.     vector <int> p(Size, -1);
  21.     d[s] = 0;
  22.  
  23.     for (int i = 0; i < m; i++) {
  24.         int u, v, weight;
  25.         cin >> u >> v >> weight;
  26.         u--;
  27.         v--;
  28.         edges[i].first = weight;
  29.         edges[i].second.first = u;
  30.         edges[i].second.second = v;
  31.     }
  32.  
  33.     int checkOnLastIt;
  34.     for (int i = 0; i < n + n; i++) {
  35.         checkOnLastIt = -1;
  36.         for (int j = 0; j < m; j++) {
  37.             if (d[edges[j].second.first] < maxen) {
  38.                 if (d[edges[j].second.second] > d[edges[j].second.first] + edges[j].first) {
  39.                     d[edges[j].second.second] = max(-maxen, d[edges[j].second.first] + edges[j].first);
  40.                     p[edges[j].second.second] = edges[j].second.first;
  41.                     checkOnLastIt = edges[j].second.second;
  42.                     if (i > n - 1) {
  43.                         path.push_back(checkOnLastIt);
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     /*
  50.         for (int i = 0; i < path.size(); i++) {
  51.             cout << path[i] << ' ';
  52.         }
  53.         return 0;
  54.         if (checkOnLastIt == -1){
  55.  
  56.  
  57.             cout << "Negative cycle: ";
  58.             for (int  i = 0; i < path.size(); i++) {
  59.                 cout << path[i] << ' ';
  60.             }
  61.  
  62.  
  63.          } else {
  64.             int otherCheckOnLastIt = checkOnLastIt;
  65.             for (int i = 0; i < n; i++) {
  66.                 otherCheckOnLastIt = p[otherCheckOnLastIt];
  67.             }
  68.  
  69.             for (int cur = otherCheckOnLastIt; ; cur = p[cur]) {
  70.                 path.push_back(cur);
  71.                 if (cur == otherCheckOnLastIt && path.size() > 1) {
  72.                     break;
  73.                 }
  74.             }
  75.  
  76.             reverse (path.begin(), path.end());
  77.         }
  78.     */
  79.     bool checkCont = true;
  80.     for (int i = 0; i < n; i++) {
  81.         for (int j = 0; j < path.size(); j++) {
  82.             if (path[j] == i) {
  83.                 cout << '-' << '\n';
  84.                 checkCont = false;
  85.                 break;
  86.  
  87.             }
  88.         }
  89.         if (!checkCont) {
  90.             checkCont = true;
  91.             continue;
  92.         }
  93.  
  94.         if (d[i] != maxen) {
  95.             cout << d[i] << '\n';
  96.         } else {
  97.             cout << '*' << '\n';
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement