Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. const int INF = 1e9;
  6.  
  7. struct edge {
  8. int a, b, cost;
  9. };
  10. vector<edge> e;
  11.  
  12. int32_t main() {
  13. cin.tie(0);
  14. ios_base::sync_with_stdio(false);
  15. freopen("negcycle.in", "r", stdin);
  16. freopen("negcycle.out", "w", stdout);
  17. int n;
  18. cin >> n;
  19. for(int i = 0; i < n; i ++)
  20. for(int j = 0; j < n; j ++)
  21. {
  22. int c;
  23. cin >> c;
  24. if(c != INF)
  25. e.push_back({i, j, c});
  26. }
  27.  
  28. vector<int> d(n);
  29. vector<int> p(n, -1);
  30. int x;
  31. for(int i = 0; i < n; i++) {
  32. x = -1;
  33. for(int j = 0; j < e.size(); j++)
  34. if (d[e[j].b] > d[e[j].a] + e[j].cost) {
  35. d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
  36. p[e[j].b] = e[j].a;
  37. x = e[j].b;
  38. }
  39. }
  40. if (x == -1)
  41. cout << "NO\n";
  42. else {
  43. int y = x;
  44. for (int i=0; i<n; ++i)
  45. y = p[y];
  46. vector<int> path;
  47. for (int cur=y; ; cur=p[cur]) {
  48. path.push_back (cur);
  49. if (cur == y && path.size() > 1) break;
  50. }
  51. reverse (path.begin(), path.end());
  52. cout << "YES\n";
  53. cout << path.size() << "\n";
  54. for(int i=0; i<path.size(); ++i)
  55. cout << path[i] + 1 << ' ';
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement