Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- int n;
- const int MX=5e3+4;
- struct node
- {
- int node;
- ll dis;
- int num;
- };
- int t,k;
- vector<pair<int,int> > adj[MX];
- priority_queue<node> pq;
- bool operator <(const node &x,const node &y)
- {
- if(x.num<y.num)return 1;
- if(x.dis>y.dis)return 1;
- return 0;
- }
- pair<int,ll> dp[MX];
- int ans;
- int opt[MX];
- pair<int,ll> solve(int x)
- {
- pair<int,ll> ans={0,0};
- if(dp[x].first!=-1)return dp[x];
- if(adj[x].size()==0)
- if(x!=n)return {-1e5,-1e5};
- int oo=0;
- for(auto u :adj[x])
- {
- auto v= solve(u.first);
- v.first++;
- v.second += u.second;
- if(v.second>t)continue;
- if(v>ans)oo=u.first;
- ans=max(ans,v);
- }
- opt[x]=oo;
- return dp[x]=ans;
- }
- int main()
- {
- cin>>n>>k>>t;
- for(int i=0;i<MX;i++)dp[i].first=dp[i].second=-1;
- for(int i=0;i<k;i++)
- {
- int x,y,z;
- scanf("%d%d%d",&x,&y,&z);
- adj[x].push_back({y,z});
- }
- ans=solve(1).first+1;
- cout<<ans<<endl;
- int x=1;
- while(ans--)
- {
- cout<<x<<' ';
- x=opt[x];
- if(x==0)return 0;
- }
- /* pq.push({1,0,0});
- int ans=0;
- node xx;
- while(!pq.empty())
- {
- xx=pq.top();
- int x=xx.node;
- int y=xx.dis;
- int nu=xx.num;
- // cout<<x<<endl;
- pq.pop();
- if (y>t)continue;
- if(x==n)break;
- if(vis[xx])continue;
- vis[xx]=1;
- for(auto u : adj[x])
- {
- pq.push({u.first,y+u.second,nu+1});
- pa[{u.first,y+u.second,nu+1}]=xx;
- }
- }
- cout<<ans+1<<endl;
- for(int i=0;i<=ans;i++)
- {
- int ret=xx.node;
- cout<<ret;
- xx=pa[xx];
- if(xx.node==0)break;
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement