Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool vis[100010];//stores visiting
  4. vector<vector<pair<long long,long long> > >v(100010);//Adjlist
  5. long long arr[100010];//stores array A
  6. long long maxval=-1;//not zero because answer may be 0 and conflict with the not possible case
  7. long long s;//collect from input,max time to take
  8. void dfs(long long node,long long val,long long dist){
  9.     if (dist>s)return;//exceed max dist
  10.     if (vis[node]){ //check for cycle
  11.         if (node==1){ //if cycle leads to
  12.             maxval=max(maxval,val);
  13.         }
  14.         return;
  15.     }
  16.     vis[node]=1;
  17.     for (long long i=0;i<v[node].size();i++){
  18.         dfs(v[node][i].first,val+arr[v[node][i].first],dist+v[node][i].second);
  19.     }
  20.     vis[node]=0;
  21.     return;
  22. }
  23. int main() {
  24.     long long n,e,i,i1,x,y,w;
  25.     cin>>n>>e>>s;
  26.     for (i=1;i<=n;i++){//1-indexed
  27.         cin>>arr[i];
  28.     }
  29.     for (i=0;i<e;i++){
  30.         cin>>x>>y>>w;
  31.         v[x].push_back({y,w});
  32.     }
  33.     dfs(1,0,0);
  34.     if (maxval!=-1)cout<<maxval<<'\n';
  35.     else cout<<"Mr Beancurd is doomed!!!\n";
  36.    
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement