DontCallMeNuttoPleas

Tower<Dijkstra>

Mar 21st, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4.     int W,V,E,fr,to;
  5.     scanf("%d%d%d",&W,&V,&E);
  6.     vector<int> v[V+10];
  7.     while(E--){
  8.         scanf("%d%d",&fr,&to);
  9.         v[fr].push_back(to);
  10.     }
  11.     vector<int> dis(V+10,2e9);
  12.     vector<bool> visited(V+10,false);
  13.     priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  14.     pq.push({0,1});
  15.     dis[1]=0;
  16.     int mx=1;
  17.     while(!pq.empty()){
  18.         int curr=pq.top().second;
  19.         pq.pop();
  20.         visited[curr]=true;
  21.         for(auto x:v[curr]){
  22.             if(!visited[x]&&dis[x]>dis[curr]+1&&W>=dis[curr]+1){
  23.                 mx=max(mx,x);
  24.                 dis[x]=dis[curr]+1;
  25.                 pq.push({dis[x],x});
  26.             }
  27.         }
  28.     }
  29.     printf("%d",mx);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment