Advertisement
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";
- }
- int n=0,m=0;
- struct ed{
- int fr, to, w;
- };
- vector <ed> G;
- const ll inf = 1e18;
- vector <ll> dst, check;
- void frd(){
- dst.resize(n, inf);
- dst[0] = 0;
- for (int l = 0; l < n; ++l){
- // cout<<"l = "<<l<<"\n";
- for (int i = 0; i < m; ++i){
- if (dst[G[i].fr] == inf) continue;
- if (dst[G[i].to] > dst[G[i].fr] + G[i].w){
- dst[G[i].to] = dst[G[i].fr] + G[i].w;
- }
- }
- }
- }
- vector <int> clr;
- vector <bool> btr; //эту вершинку пересчитали
- vector <vector <int> > goG;
- vector <int> tpt;
- vector <int> ans;
- void dfs1(int v){
- clr[v] = 1;
- for (int u: goG[v]){
- if (clr[u] == 0){
- dfs1(u);
- }
- }
- clr[v] = 2;
- tpt.push_back(v);
- }
- void topsort(){
- clr.assign(n, 0);
- for (int i = 0; i < n; ++i){
- if (clr[i] == 0) dfs1(i);
- }
- }
- void dfs2(int v){
- clr[v] = 1;
- for (int u: goG[v]){
- if (clr[u] == 0 && btr[u] == 1){
- dfs2(u);
- }
- }
- clr[v] = 2;
- ans.push_back(v+1);
- }
- void chk(){
- check = dst;
- for (int rep = 0; rep < 2;++rep){
- for (int l = 0; l < n; ++l){
- for (int i = 0; i < m;++i){
- if (check[G[i].fr] == inf) continue;
- if (check[G[i].to] > check[G[i].fr] + G[i].w){
- check[G[i].to] = check[G[i].fr] + G[i].w;
- }
- }
- }
- }
- topsort();
- clr.assign(n, 0);
- btr.assign(n, 0);
- for (int i=0;i<n;++i){
- if (check[i] < dst[i]){
- btr[i] = 1;
- }
- }
- for (int i = 0; i < n;++i){
- if (btr[i]){
- dfs2(i);
- cout<<"YES\n";
- cv(ans);
- cout<<ans[0]<<"\n";
- exit(0);
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n;
- //G.resize(m);
- goG.resize(n);
- for (int i = 0; i < n;++i){
- for (int j = 0; j < n;++j){
- int d; cin>>d; if (d == 100000) continue;
- m++;
- goG[i].push_back(j);
- ed any = {i, j, d};
- G.push_back(any);
- }
- }
- frd();
- chk();
- cout<<"NO\n";
- }
- /*
- 3
- 100000 100000 -51
- 100 100000 100000
- 100000 -50 100000
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement