Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. //  Copyright © 2017 Adán López Alatorre. All rights reserved.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7. #include <deque>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <set>
  11. #include <map>
  12. #include <sstream>
  13. #include <stack>
  14. #include <iomanip>
  15. #include <fstream>
  16. #include <climits>
  17. #include <cmath>
  18. #define fi first
  19. #define se second
  20. #define FI first.first
  21. #define SE first.second
  22. #define TH second
  23. #define MAXN maxN
  24.  
  25. using namespace std;
  26.  
  27. typedef long long ll;
  28. typedef pair<int, int> ii;
  29. typedef pair<ii, int> iii;
  30. typedef pair<double, int> di;
  31. typedef vector<int> vi;
  32. typedef set<ii>::iterator iter;
  33.  
  34. const int maxN = 2e3 + 3, MOD = 1e9 + 7, AL = 10;
  35.  
  36. int toh[maxN], frh[maxN];
  37. bool mat[maxN][maxN];
  38.  
  39. vector<int> adj[maxN];
  40.  
  41. void bfs(int s, int *arr){
  42.     queue<ii> q;
  43.     q.push(ii(s, 0));
  44.    
  45.     memset(arr, -1, sizeof(toh));
  46.     arr[s] = 0;
  47.    
  48.     while(q.size()){
  49.         ii cur = q.front(); q.pop();
  50.        
  51.         for(int sn: adj[cur.fi])
  52.             if(arr[sn] == -1)
  53.                 arr[sn] = cur.se + 1, q.push(ii(sn, cur.se + 1));
  54.     }
  55.    
  56. }
  57.  
  58. int main(){
  59.     ios::sync_with_stdio(false);
  60.     cin.tie(0), cout.tie(0);
  61.    
  62.     int n, m, s, t;
  63.     cin >> n >> m >> s >> t;
  64.     s--, t--;
  65.    
  66.     for(int i = 0, u, v; i < m; i++){
  67.         cin >> u >> v;
  68.         u--, v--;
  69.         adj[u].push_back(v), adj[v].push_back(u);
  70.         mat[u][v] = mat[v][u] = true;
  71.     }
  72.    
  73.     bfs(s, toh), bfs(t, frh);
  74.    
  75.     int res = 0;
  76.    
  77.     for(int i = 0; i < n; i++){
  78.         for(int j = i + 1; j < n; j++){
  79.             if(mat[i][j]) continue;
  80.             int bes = min(toh[i] + frh[j] + 1, toh[j] + frh[i] + 1);
  81.             if(bes >= toh[t]) res++;
  82.         }
  83.     }
  84.     cout << res << '\n';
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement