Advertisement
Vince14

/<> 1348 (maximum bipartite matching + bfs + binary search)

Nov 11th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<long long, long long>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 1005;
  23.  
  24. int r, c;
  25. int carn = 0, parkn = 0;
  26. int park[55][55]; // . = 0, X = -1, C = 1, P = 2 + 0, 2 + 1, ...
  27. bool visited[55][55];
  28. int dist[105][105];
  29. vector<pii> car;
  30. vector<bool> cars;
  31. vector<int> parks;
  32. vector<pii> v[105];
  33.  
  34. void printdist(){
  35.     for(int i = 0; i < 10; i++){
  36.         cout << i << ": ";
  37.         for(int j = 0; j < 10; j++){
  38.             cout << dist[i][j] << " ";
  39.         }
  40.         cout << "\n";
  41.     }
  42. }
  43.  
  44. void init(){
  45.     cin >> r >> c;
  46.     int tmp = 0;
  47.     for(int i = 0; i < r; i++){
  48.         for(int j = 0; j < c; j++){
  49.             char cc;
  50.             cin >> cc;
  51.             if(cc == '.'){
  52.                 park[i][j] = 0;
  53.             }
  54.             else if(cc == 'X'){
  55.                 park[i][j] = -1;
  56.             }
  57.             else if(cc == 'C'){
  58.                 park[i][j] = -2;
  59.                 car.push_back(make_pair(i, j));
  60.                 carn++;
  61.             }
  62.             else{
  63.                 park[i][j] = 2 + tmp;
  64.                 tmp++;
  65.                 parkn++;
  66.             }
  67.         }
  68.     }
  69.     queue<pair<pii, int>> q;
  70.     for(int k = 0; k < car.size(); k++){
  71.         pii coord = car[k];
  72.         for(int i = 0; i < 55; i++){
  73.             for(int j = 0; j < 55; j++){
  74.                 visited[i][j] = false;
  75.             }
  76.         }
  77.         q.push(make_pair(coord, 0));
  78.         while(!q.empty()){
  79.             int cx = q.front().first.first;
  80.             int cy = q.front().first.second;
  81.             int cd = q.front().second;
  82.             q.pop();
  83.             if(visited[cx][cy]){
  84.                 continue;
  85.             }
  86.             visited[cx][cy] = true;
  87.             if(park[cx][cy] >= 2){
  88.                 dist[k][park[cx][cy] - 2] = cd;
  89.             }
  90.             for(int i = 0; i < 4; i++){
  91.                 int nx = cx + dx[i];
  92.                 int ny = cy + dy[i];
  93.                 if(nx < 0 || nx >= r || ny < 0 or ny >= c || visited[nx][ny] || park[nx][ny] == -1){
  94.                     continue;
  95.                 }
  96.                 q.push({{nx, ny}, cd + 1});
  97.             }
  98.         }
  99.     }
  100. }
  101.  
  102. bool dfs(int cur, int x){
  103.     if(cars[cur]){
  104.         return false;
  105.     }
  106.     cars[cur] = true;
  107.     for(auto nxt : v[cur]){
  108.         if(nxt.second > x){
  109.             continue;
  110.         }
  111.         if(parks[nxt.first] == -1 || dfs(parks[nxt.first], x)){
  112.             parks[nxt.first] = cur;
  113.             return true;
  114.         }
  115.     }
  116.     return false;
  117. }
  118.  
  119. bool match(int x){
  120.     int parked = 0;
  121.     parks.assign(parkn, -1);
  122.     for(int i = 0; i < carn; i++){
  123.         cars.assign(carn, false);
  124.         if(dfs(i, x)){
  125.             parked++;
  126.         }
  127.     }
  128.     if(parked == carn){
  129.         return true;
  130.     }
  131.     else{
  132.         return false;
  133.     }
  134. }
  135.  
  136.  
  137. void solve(){
  138.     if(carn == 0){
  139.         cout << "0\n";
  140.         return;
  141.     }
  142.     if(carn > parkn){
  143.         cout << "-1\n";
  144.         return;
  145.     }
  146.     int maxt = 0;
  147.     for(int i = 0; i < carn; i++){
  148.         for(int j = 0; j < parkn; j++){
  149.             if(dist[i][j] > 0){
  150.                 maxt = max(maxt, dist[i][j]);
  151.                 v[i].push_back(make_pair(j, dist[i][j]));
  152.             }
  153.         }
  154.     }
  155.     int lo = 0, hi = maxt;
  156.     if(!match(maxt)){
  157.         cout << "-1\n";
  158.         return;
  159.     }
  160.     while(lo < hi){
  161.         int mid = (lo + hi) / 2;
  162.         bool ck = match(mid);
  163.         if(!ck){
  164.             lo = mid + 1;
  165.         }
  166.         else{
  167.             hi = mid;
  168.         }
  169.     }
  170.     cout << lo << "\n";
  171. }
  172.  
  173.  
  174. int main() {
  175.     FAST;
  176.     init();
  177.     solve();
  178. }
  179.  
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement