Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. О╩©#include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct edge {
  6. int a, b, cost;
  7. };
  8.  
  9. int n, m;
  10. vector<edge> e;
  11. const int INF = 1000000000;
  12.  
  13. void solve()
  14. {
  15. vector<int> d (n);
  16. vector<int> p (n, -1);
  17. int x;
  18. for (int i=0; i<n; ++i)
  19. {
  20. x = -1;
  21. for (int j=0; j<m; ++j)
  22. if (d[e[j].b] > d[e[j].a] + e[j].cost)
  23. {
  24. d[e[j].b] = max (-INF, d[e[j].a] + e[j].cost);
  25. p[e[j].b] = e[j].a;
  26. x = e[j].b;
  27. }
  28. }
  29.  
  30. if (x == -1)
  31. cout << "NO";
  32. else {
  33. int y = x;
  34. for (int i=0; i<n; ++i)
  35. y = p[y];
  36.  
  37. vector<int> path;
  38. for (int cur=y; ; cur=p[cur])
  39. {
  40. path.push_back (cur);
  41. if (cur == y && path.size() > 1) break;
  42. }
  43. reverse (path.begin(), path.end());
  44.  
  45. cout << "YES" << endl << path.size()-1 << endl;
  46. for (size_t i=0; i<path.size()-1; ++i)
  47. cout << path[i]+1 << ' ';
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. #ifdef AT_MY_PC
  54. freopen("input.txt", "r", stdin);
  55. #endif
  56. ios::sync_with_stdio(0);
  57. cin.tie(nullptr);
  58.  
  59. cin >> n;
  60. for(int i = 0; i < n; ++i)
  61. for(int j = 0; j < n; ++j)
  62. {
  63. int t;
  64. cin >> t;
  65. edge tmp;
  66. tmp.a = i;
  67. tmp.b = j;
  68. tmp.cost = t;
  69. if(t != 0 && t != 100000)
  70. e.push_back(tmp);
  71. }
  72. m = e.size();
  73. solve();
  74.  
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement