fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 1e5 + 7;
  4. vector <int>a[MAXN];
  5. int n, m, dist[MAXN], k, p[MAXN];
  6. queue <int> q;
  7.  
  8. int main(){
  9. ios_base::sync_with_stdio(0);
  10. cout.tie(0);
  11. cin.tie(0);
  12. cin >> n >> m >> k;
  13. for(int i = 1; i <= k ; i++) cin >> p[i];
  14. for(int i = 1; i <= m; i++){
  15. int x, y;
  16. cin >> x >> y;
  17. a[x].push_back(y);
  18. a[y].push_back(x);
  19. }
  20. fill(dist + 1, dist + 1 + n, INT_MAX);
  21. q.push(n);
  22. dist[n] = 0;
  23. while(!q.empty()){
  24. int u = q.front();
  25. q.pop();
  26. for(auto v : a[u]){
  27. if(dist[v] > dist[u] + 1){
  28. dist[v] = dist[u] + 1;
  29. q.push(v);
  30. }
  31. }
  32. }
  33. for(int i = 1; i <= k; i++) cout << dist[p[i]] << ' ';
  34.  
  35. }
Success #stdin #stdout 0.01s 5884KB
stdin
Standard input is empty
stdout
Standard output is empty