Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct edge
- {
- long long f, s, cost;
- };
- vector <edge> v;
- long long n, m, s, x;
- ifstream ccin("negcycle.in");
- ofstream ccout("negcycle.out");
- int main()
- {
- ccin >> n;
- for (long long i = 0; i<n; i++)
- {
- for (long long j = 0; j<n; j++)
- {
- edge t;
- ccin >> t.cost;
- if (t.cost != 100000)
- {
- t.f = i;
- t.s = j;
- v.push_back(t);
- }
- }
- }
- vector <long long> len(n, 6000000000000000000), path(n, -1);
- len[0] = 0;
- for (int i = 0; i<2 * n; i++)
- {
- x = -1;
- for (int j = 0; j<v.size(); j++)
- {
- if (len[v[j].f] < 6000000000000000000)
- {
- if (len[v[j].s] > len[v[j].f] + v[j].cost)
- {
- len[v[j].s] = max(-7000000000000000000, len[v[j].f] + v[j].cost);
- path[v[j].s] = v[j].f;
- x = v[j].f;
- }
- }
- }
- }
- if (x == -1)
- {
- ccout << "NO";
- }
- else
- {
- ccout << "YES" << '\n';
- long long y = x;
- for (int i = 0; i<n; i++)
- y = path[y];
- vector <int> ans;
- for (long long j = y;; j = path[j])
- {
- if (j == y && ans.size() > 1)
- break;
- ans.push_back(j);
- }
- ccout << ans.size() << '\n';
- for (int j = ans.size() - 1; j >= 0; j--)
- ccout << ans[j] + 1 << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement