Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- О╩©#include <bits/stdc++.h>
- using namespace std;
- struct edge {
- int a, b, cost;
- };
- int n, m;
- vector<edge> e;
- const int INF = 1000000000;
- void solve()
- {
- vector<int> d (n);
- vector<int> p (n, -1);
- int x;
- for (int i=0; i<n; ++i)
- {
- x = -1;
- for (int j=0; j<m; ++j)
- if (d[e[j].b] > d[e[j].a] + e[j].cost)
- {
- d[e[j].b] = max (-INF, d[e[j].a] + e[j].cost);
- p[e[j].b] = e[j].a;
- x = e[j].b;
- }
- }
- if (x == -1)
- cout << "NO";
- else {
- int y = x;
- for (int i=0; i<n; ++i)
- y = p[y];
- vector<int> path;
- for (int cur=y; ; cur=p[cur])
- {
- path.push_back (cur);
- if (cur == y && path.size() > 1) break;
- }
- reverse (path.begin(), path.end());
- cout << "YES" << endl << path.size()-1 << endl;
- for (size_t i=0; i<path.size()-1; ++i)
- cout << path[i]+1 << ' ';
- }
- }
- int main()
- {
- #ifdef AT_MY_PC
- freopen("input.txt", "r", stdin);
- #endif
- ios::sync_with_stdio(0);
- cin.tie(nullptr);
- cin >> n;
- for(int i = 0; i < n; ++i)
- for(int j = 0; j < n; ++j)
- {
- int t;
- cin >> t;
- edge tmp;
- tmp.a = i;
- tmp.b = j;
- tmp.cost = t;
- if(t != 0 && t != 100000)
- e.push_back(tmp);
- }
- m = e.size();
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement