Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int W,V,E,fr,to;
- scanf("%d%d%d",&W,&V,&E);
- vector<int> v[V+10];
- while(E--){
- scanf("%d%d",&fr,&to);
- v[fr].push_back(to);
- }
- vector<int> dis(V+10,2e9);
- vector<bool> visited(V+10,false);
- priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
- pq.push({0,1});
- dis[1]=0;
- int mx=1;
- while(!pq.empty()){
- int curr=pq.top().second;
- pq.pop();
- visited[curr]=true;
- for(auto x:v[curr]){
- if(!visited[x]&&dis[x]>dis[curr]+1&&W>=dis[curr]+1){
- mx=max(mx,x);
- dis[x]=dis[curr]+1;
- pq.push({dis[x],x});
- }
- }
- }
- printf("%d",mx);
- }
Advertisement
Add Comment
Please, Sign In to add comment