Advertisement
Josif_tepe

Untitled

Jan 23rd, 2023
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <set>
  6. #include <cstring>
  7. using namespace std;
  8.  
  9. const int maxn = 55;
  10. int n, m;
  11. char mat[maxn][maxn];
  12. int dist[maxn][maxn][maxn][maxn];
  13. bool visited[maxn][maxn];
  14. vector<pair<int, int> > tables, customers;
  15. void bfs(int si, int sj) {
  16.     memset(visited, false, sizeof visited);
  17.    
  18.     visited[si][sj] = true;
  19.     queue<int> Q;
  20.     Q.push(si);
  21.     Q.push(sj);
  22.     Q.push(0);
  23.    
  24.     int di[] = {-1, 1, 0, 0};
  25.     int dj[] = {0, 0, -1, 1};
  26.     while(!Q.empty()) {
  27.         int ci = Q.front();
  28.         Q.pop();
  29.         int cj = Q.front();
  30.         Q.pop();
  31.         int d = Q.front();
  32.         Q.pop();
  33.        
  34.         if(mat[ci][cj] == 'M') {
  35.             dist[si][sj][ci][cj] = d;
  36.         }
  37.        
  38.         for(int i = 0; i < 4; i++) {
  39.             int ti = ci + di[i];
  40.             int tj = cj + dj[i];
  41.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != '#') {
  42.                 Q.push(ti);
  43.                 Q.push(tj);
  44.                 Q.push(d + 1);
  45.                 visited[ti][tj] = true;
  46.             }
  47.         }
  48.     }
  49. }
  50. int graph[111][111];
  51. int match[111];
  52. bool vis[111];
  53. bool dfs(int node) {
  54.     for(int i = 0; i < tables.size(); i++) {
  55.         if(graph[node][i] == 1 and !vis[i]) {
  56.             vis[i] = true;
  57.             if(match[i] == -1 or dfs(match[i])) {
  58.                 match[i] = node;
  59.                 return true;
  60.             }
  61.         }
  62.     }
  63.     return false;
  64. }
  65. bool check(int distance) {
  66.     memset(graph, 0, sizeof graph);
  67.     for(int i = 0; i < customers.size(); i++) {
  68.         for(int j = 0; j < tables.size(); j++) {
  69.             if(dist[customers[i].first][customers[i].second][tables[j].first][tables[j].second] <= distance) {
  70.                 graph[i][j] = 1;
  71.             }
  72.         }
  73.     }
  74.     memset(match, -1, sizeof match);
  75.     int matched = 0;
  76.     for(int i = 0; i < customers.size(); i++) {
  77.         memset(vis, false, sizeof vis);
  78.         if(dfs(i)) {
  79.             matched++;
  80.         }
  81.     }
  82.     return (matched >= (int) customers.size());
  83. }
  84.  
  85. int main()
  86. {
  87.     cin >> n >> m;
  88.     for(int i = 0; i < n; i++) {
  89.         for(int j = 0; j < m; j++) {
  90.             cin >> mat[i][j];
  91.         }
  92.     }
  93.     for(int i = 0; i < n; i++) {
  94.         for(int j = 0; j < m; j++) {
  95.             for(int x = 0; x < n; x++) {
  96.                 for(int y = 0; y < m; y++) {
  97.                     dist[i][j][x][y] = 2e9;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     for(int i = 0; i < n; i++) {
  103.         for(int j = 0; j < m; j++) {
  104.             if(mat[i][j] == 'P') {
  105.                 bfs(i, j);
  106.                 customers.push_back(make_pair(i, j));
  107.             }
  108.             if(mat[i][j] == 'M') {
  109.                 tables.push_back(make_pair(i, j));
  110.             }
  111.         }
  112.     }
  113.    
  114.     int L = 0, R = 3333;
  115.     int result = 2e9;
  116.     while(L <= R) {
  117.         int middle = (L + R) / 2;
  118.         if(check(middle)) {
  119.             result = min(result, middle);
  120.             R = middle - 1;
  121.         }
  122.         else {
  123.             L = middle + 1;
  124.         }
  125.     }
  126.     cout << result << endl;
  127.    
  128.     return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement