Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ��#include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <set>
- #include <stack>
- #include <queue>
- #include <deque>
- using namespace std;
- #define TASK "negcycle"
- const long long INF = 1e18;
- struct edge {
- long long a, b, w;
- edge() {}
- edge(long long aa, long long bb, long long ww) { a = aa, b = bb, w = ww; }
- };
- long long n, m, v, x;
- vector < edge > e;
- vector < long long > ans, d(300, INF), p(300, -1);
- void ford() {
- d[v] = 0;
- for (int i = 0; i < n; i++) {
- x = -1;
- for (int j = 0; j < m; j++) {
- if (d[e[j].a] < INF) {
- if (d[e[j].a] + e[j].w < d[e[j].b]) {
- d[e[j].b] = max(-INF, d[e[j].a] + e[j].w);
- p[e[j].b] = e[j].a;
- if (i == n - 1) {
- x = e[j].b;
- }
- }
- }
- }
- }
- if (x == -1) {
- cout << "NO";
- return;
- }
- for (int i = 0; i < n; i++) {
- x = p[x];
- }
- for (int i = x; ; i = p[i]) {
- ans.push_back(i + 1);
- if (i == x && ans.size() > 1) {
- break;
- }
- }
- cout << "YES\n" << ans.size() << "\n";
- for (int i = ans.size() - 1; i >= 0; i--) {
- cout << ans[i] << " ";
- }
- }
- int main() {
- #ifdef _DEBUG
- freopen("debug.in", "r", stdin);
- freopen("debug.out", "w", stdout);
- #else
- freopen(TASK".in", "r", stdin);
- freopen(TASK".out", "w", stdout);
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif // _DEBUG
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout.precision(6);
- long long a;
- cin >> n;
- v = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cin >> a;
- if (a != 100000) {
- e.push_back(edge(i, j, a));
- }
- }
- }
- m = e.size();
- ford();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement