Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- struct ed{
- int fr, to, w;
- };
- int inf = 2e9;
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n; cin>>n;
- int m = 0;
- vector <int> d(n, 0);
- vector <ed> G;
- vector <int> pred(n);
- for (int i = 0; i < n ;++i){
- for (int j = 0; j < n;++j){
- int ds; cin>>ds;
- if (ds == 100000) continue;
- ed x = {i, j, ds};
- m++;
- G.push_back(x);
- }
- }
- for (ed x: G){
- // cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
- }
- //cout<<"m = "<<m<<"\n";
- for (int i = 0; i < n; ++i){
- for (int j = 0; j < m; ++j){
- ed x = G[j];
- if (d[x.fr] == inf) continue;
- if (d[x.to] > d[x.fr] + x.w){
- d[x.to] = d[x.fr] + x.w;
- pred[x.to] = x.fr;
- }
- }
- }
- vector <bool> upd(n, 0);
- for (int j = 0; j < m; ++j){
- ed x = G[j];
- if (d[x.fr] == inf) continue;
- if (d[x.to] > d[x.fr] + x.w){
- upd[x.to] = 1;
- d[x.to] = d[x.fr] + x.w;
- pred[x.to] = x.fr;
- }
- }
- for (int i = 0; i < n;++i){
- if (upd[i]){
- int v = i;
- for (int j = 0; j < n+1;++j){
- v = pred[v];
- }
- int str = v;
- vector <int> ans = {str};
- v = pred[v];
- while (v != str){
- ans.push_back(v);
- v = pred[v];
- }
- reverse(ans.begin() , ans.end());
- ans.push_back(ans[0]);
- cout<<"YES\n";
- cout<<ans.size()<<"\n";
- for (int v: ans) cout<<v+1<<' ';
- exit(0);
- }
- }
- cout<<"NO\n";
- }
Add Comment
Please, Sign In to add comment