File tree Expand file tree Collapse file tree 1 file changed +91
-0
lines changed
Expand file tree Collapse file tree 1 file changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ // JAI BAJARANG BALI
2+
3+ // manitianajay45
4+
5+ // give me some sunshine, give me some rain, give me another chance to grow up once again....
6+
7+ // sab moh maya hai....
8+
9+ #include < bits/stdc++.h>
10+ using namespace std ;
11+
12+ #define ll long long
13+ #define INF 1e15
14+ ll n,m;
15+
16+ struct ed
17+ {
18+ ll u,v,c;
19+ };
20+
21+ vector<ll> graphf[2505 ];
22+ vector<ll> graphr[2505 ];
23+ vector<bool > vis1 (2505 ,false );
24+ vector<bool > visn (2505 ,false );
25+
26+ void dfs (ll v){
27+ vis1[v]=true ;
28+ for (auto u:graphf[v]){
29+ if (!vis1[u]){
30+ dfs (u);
31+ }
32+ }
33+ }
34+ void dfs1 (ll v){
35+ visn[v]=true ;
36+ for (auto u:graphr[v]){
37+ if (!visn[u]){
38+ dfs1 (u);
39+ }
40+ }
41+ }
42+
43+ int main ()
44+ {
45+ ios_base::sync_with_stdio (false );
46+ cin.tie (NULL );
47+ cin >> n >> m;
48+
49+ vector<ed> edges;
50+ for (ll i = 0 ; i < m; i++)
51+ {
52+ ll u, v, c;
53+ cin >> u >> v >> c;
54+ graphf[u].push_back (v);
55+ graphr[v].push_back (u);
56+ edges.push_back ({u,v,c});
57+ }
58+ // cout<<"YES"<<endl;
59+
60+ dfs (1 );
61+ dfs1 (n);
62+ vector<ll> d (n+1 ,INT_MIN);
63+ d[1 ]=0 ;
64+ ll ans=-1 ;
65+ bool flag=false ;
66+ for (ll j=0 ;j<n;j++){
67+ flag=false ;
68+ for (ll i=0 ;i<m;i++){
69+ ed nd=edges[i];
70+ if (d[nd.u ]>INT_MIN){
71+ if (vis1[nd.v ] && visn[nd.v ] && d[nd.v ]< d[nd.u ]+nd.c ){
72+ d[nd.v ]= d[nd.u ]+nd.c ;
73+ flag=true ;
74+ }
75+ }
76+ }
77+ if (j==n-1 ){
78+ ans=d[n];
79+ }
80+
81+ }
82+ if (flag || d[n]==INT_MIN){
83+ cout<<" -1" <<endl;
84+ return 0 ;
85+ }
86+ cout<<d[n]<<endl;
87+
88+
89+
90+ return 0 ;
91+ }
You can’t perform that action at this time.
0 commit comments