Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct edge
  6. {
  7. long long f, s, cost;
  8. };
  9.  
  10. vector <edge> v;
  11.  
  12. long long n, m, s, x;
  13.  
  14. ifstream ccin("negcycle.in");
  15. ofstream ccout("negcycle.out");
  16.  
  17. int main()
  18. {
  19. ccin >> n;
  20. for (long long i = 0; i<n; i++)
  21. {
  22. for (long long j = 0; j<n; j++)
  23. {
  24. edge t;
  25. ccin >> t.cost;
  26. if (t.cost != 100000)
  27. {
  28. t.f = i;
  29. t.s = j;
  30. v.push_back(t);
  31. }
  32. }
  33. }
  34. vector <long long> len(n, 6000000000000000000), path(n, -1);
  35. len[0] = 0;
  36. for (int i = 0; i<2 * n; i++)
  37. {
  38. x = -1;
  39. for (int j = 0; j<v.size(); j++)
  40. {
  41. if (len[v[j].f] < 6000000000000000000)
  42. {
  43. if (len[v[j].s] > len[v[j].f] + v[j].cost)
  44. {
  45. len[v[j].s] = max(-7000000000000000000, len[v[j].f] + v[j].cost);
  46. path[v[j].s] = v[j].f;
  47. x = v[j].f;
  48. }
  49. }
  50. }
  51. }
  52. if (x == -1)
  53. {
  54. ccout << "NO";
  55. }
  56. else
  57. {
  58. ccout << "YES" << '\n';
  59. long long y = x;
  60. for (int i = 0; i<n; i++)
  61. y = path[y];
  62. vector <int> ans;
  63. for (long long j = y;; j = path[j])
  64. {
  65. if (j == y && ans.size() > 1)
  66. break;
  67. ans.push_back(j);
  68. }
  69. ccout << ans.size() << '\n';
  70. for (int j = ans.size() - 1; j >= 0; j--)
  71. ccout << ans[j] + 1 << " ";
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement