Advertisement
NHumme

E

Apr 7th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. ��#include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "negcycle"
  14.  
  15. const long long INF = 1e18;
  16.  
  17. struct edge {
  18.     long long a, b, w;
  19.     edge() {}
  20.     edge(long long aa, long long bb, long long ww) { a = aa, b = bb, w = ww; }
  21. };
  22.  
  23. long long n, m, v, x;
  24. vector < edge > e;
  25. vector < long long > ans, d(300, INF), p(300, -1);
  26.  
  27. void ford() {
  28.     d[v] = 0;
  29.     for (int i = 0; i < n; i++) {
  30.         x = -1;
  31.         for (int j = 0; j < m; j++) {
  32.             if (d[e[j].a] < INF) {
  33.                 if (d[e[j].a] + e[j].w < d[e[j].b]) {
  34.                     d[e[j].b] = max(-INF, d[e[j].a] + e[j].w);
  35.                     p[e[j].b] = e[j].a;
  36.                     if (i == n - 1) {
  37.                         x = e[j].b;
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     if (x == -1) {
  44.         cout << "NO";
  45.         return;
  46.     }
  47.     for (int i = 0; i < n; i++) {
  48.         x = p[x];
  49.     }
  50.     for (int i = x; ; i = p[i]) {
  51.         ans.push_back(i + 1);
  52.         if (i == x && ans.size() > 1) {
  53.             break;
  54.         }
  55.     }
  56.     cout << "YES\n" << ans.size() << "\n";
  57.     for (int i = ans.size() - 1; i >= 0; i--) {
  58.         cout << ans[i] << " ";
  59.     }
  60. }
  61.  
  62. int main() {
  63.  
  64. #ifdef _DEBUG
  65.     freopen("debug.in", "r", stdin);
  66.     freopen("debug.out", "w", stdout);
  67. #else
  68.     freopen(TASK".in", "r", stdin);
  69.     freopen(TASK".out", "w", stdout);
  70.     //freopen("input.txt", "r", stdin);
  71.     //freopen("output.txt", "w", stdout);
  72. #endif // _DEBUG
  73.  
  74.     ios_base::sync_with_stdio(0);
  75.     cin.tie(0);
  76.     cout.tie(0);
  77.     cout.precision(6);
  78.  
  79.     long long a;
  80.     cin >> n;
  81.     v = 0;
  82.     for (int i = 0; i < n; i++) {
  83.         for (int j = 0; j < n; j++) {
  84.             cin >> a;
  85.             if (a != 100000) {
  86.                 e.push_back(edge(i, j, a));
  87.             }
  88.         }
  89.     }
  90.     m = e.size();
  91.     ford();
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement