|
| 1 | +/* |
| 2 | + IN THE NAME OF ALLAH |
| 3 | + Author: Moinul Hossain |
| 4 | + Northern University Bangladesh |
| 5 | +*/ |
| 6 | +#include<bits/stdc++.h> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | + |
| 10 | +typedef long long ll; |
| 11 | +typedef vector<int> vi; |
| 12 | +typedef vector<ll> vl; |
| 13 | +typedef vector<vi> vvi; |
| 14 | +typedef vector<vl> vvl; |
| 15 | +typedef pair<int,int> pii; |
| 16 | +typedef pair<double, double> pdd; |
| 17 | +typedef pair<ll, ll> pll; |
| 18 | +typedef vector<pii> vii; |
| 19 | +typedef vector<pll> vll; |
| 20 | +typedef double dl; |
| 21 | + |
| 22 | +#define endl '\n' |
| 23 | +#define PB push_back |
| 24 | +#define F first |
| 25 | +#define S second |
| 26 | +#define all(a) (a).begin(),(a).end() |
| 27 | +#define rall(a) (a).rbegin(),(a).rend() |
| 28 | +#define sz(x) (int)x.size() |
| 29 | + |
| 30 | +const double PI = acos(-1); |
| 31 | +const double eps = 1e-9; |
| 32 | +const int inf = 2000000000; |
| 33 | +const ll infLL = 9000000000000000000; |
| 34 | +#define MOD 1000000007 |
| 35 | + |
| 36 | +#define mem(a,b) memset(a, b, sizeof(a) ) |
| 37 | +#define sqr(a) ((a) * (a)) |
| 38 | + |
| 39 | +#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); |
| 40 | +#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); |
| 41 | +#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); |
| 42 | +// |
| 43 | +//debug |
| 44 | +template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";} |
| 45 | +template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";} |
| 46 | +template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";} |
| 47 | +template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";} |
| 48 | +template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";} |
| 49 | +#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0) |
| 50 | +void faltu(){cerr << endl;} |
| 51 | +template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;} |
| 52 | +template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);} |
| 53 | +//#else |
| 54 | +//#define dbg(args...) |
| 55 | + |
| 56 | +ll gcd ( ll a, ll b ) { return __gcd ( a, b ); } |
| 57 | +ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); } |
| 58 | + |
| 59 | + |
| 60 | +int dx[] = { 0, 0, +1, -1, -1 +1, -1, +1 }; |
| 61 | +int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 }; |
| 62 | + |
| 63 | +const int mx=2512; |
| 64 | + |
| 65 | +struct edge { |
| 66 | + int u, v, w; |
| 67 | +}; |
| 68 | + |
| 69 | +ll dist[mx]; |
| 70 | +int par[mx]; |
| 71 | +vector<edge> e; |
| 72 | + |
| 73 | +vi bellmanFord(int s, int n, int m) { |
| 74 | + vi cycle; |
| 75 | + for(int i=1; i<=n; i++) dist[i]=infLL; |
| 76 | + dist[s]=0; |
| 77 | + int x=-1; |
| 78 | + for(int i=1; i<=n; i++) { |
| 79 | + x=-1; |
| 80 | + for(int j=0; j<m; j++) { |
| 81 | + int u = e[j].u; |
| 82 | + int v = e[j].v; |
| 83 | + int w = e[j].w; |
| 84 | + |
| 85 | + if(dist[u] + w < dist[v]) { |
| 86 | + dist[v] = dist[u] + w; |
| 87 | + par[v] = u; |
| 88 | + x = v; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if(x==-1) return cycle; |
| 93 | + |
| 94 | + for(int i=1; i<=n; i++) { |
| 95 | + x=par[x]; |
| 96 | + } |
| 97 | + |
| 98 | + int y = x; |
| 99 | + while(y != x || sz(cycle) == 0) { |
| 100 | + cycle.PB(y); |
| 101 | + y=par[y]; |
| 102 | + } |
| 103 | + |
| 104 | + cycle.PB(x); |
| 105 | + reverse(all(cycle)); |
| 106 | + return cycle; |
| 107 | +} |
| 108 | + |
| 109 | +int main() { |
| 110 | + optimize(); |
| 111 | + int n, m; |
| 112 | + cin >> n >> m; |
| 113 | + for(int i=1; i<=m; i++) { |
| 114 | + int u, v, w; |
| 115 | + cin >> u >> v >> w; |
| 116 | + e.PB({ u, v, w }); |
| 117 | + } |
| 118 | + vi ans = bellmanFord(1, n, m); |
| 119 | + if(sz(ans) == 0) cout << "NO\n"; |
| 120 | + else { |
| 121 | + cout << "YES\n"; |
| 122 | + for(auto u : ans) cout << u << " "; |
| 123 | + cout << endl; |
| 124 | + } |
| 125 | +} |
0 commit comments