Advertisement
namemkazaza

Untitled

Mar 29th, 2021
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <cstdio>
  4. #define MAX 102
  5. using namespace std;
  6.  
  7.  
  8. char s[MAX][MAX];
  9. int res, n, m, a, b, i, j, c;
  10. vector<vector<int> > g;
  11. vector<int> mt, used, par;
  12. int dfs(int v)
  13. {
  14.     if (used[v]) return 0;
  15.     used[v] = 1;
  16.     for (int i = 0; i < g[v].size(); i++)
  17.     {
  18.         int to = g[v][i];
  19.         if (mt[to] == -1 || dfs(mt[to]))
  20.         {
  21.             mt[to] = v;
  22.             par[v] = to;
  23.             return 1;
  24.         }
  25.     }
  26.     return 0;
  27. }
  28.  
  29. void AugmentingPath(void)
  30. {
  31.     int i, j, run;
  32.     mt.assign(n * m, -1);
  33.     par.assign(n * m, -1);
  34.     for (run = 1; run; )
  35.     {
  36.         run = 0; used.assign(n * m, 0);
  37.         for (i = 0; i < n; i++)
  38.             for (j = 0; j < m; j++)
  39.             {
  40.                 if ((i + j) % 2) continue;
  41.                 if ((par[i * m + j] == -1) && dfs(i * m + j)) run = 1;
  42.             }
  43.     }
  44. }
  45.  
  46. int main(void)
  47. {
  48.     int empty;
  49.     scanf("%d %d %d %d\n", &n, &m, &a, &b);
  50.     for (i = 0; i < n; i++) fgets(s[i], MAX, stdin);
  51.     g.resize(n * m);
  52.     empty = 0;
  53.     for (i = 0; i < n; i++)
  54.         for (j = 0; j < m; j++)
  55.         {
  56.             if (s[i][j] == '.') continue;
  57.             empty++;
  58.             if ((i + j) % 2) continue;
  59.             int u = i * m + j;
  60.             if (j && (s[i][j - 1] == '*')) g[u].push_back(u - 1);
  61.             if ((j < m - 1) && (s[i][j + 1] == '*')) g[u].push_back(u + 1);
  62.             if (i && (s[i - 1][j] == '*')) g[u].push_back(u - m);
  63.             if ((i < n - 1) && (s[i + 1][j] == '*')) g[u].push_back(u + m);
  64.         }
  65.     if (2 * b <= a)
  66.     {
  67.         printf("%d\n", empty * b);
  68.         return 0;
  69.     }
  70.     AugmentingPath();
  71.     for (c = i = 0; i < n * m; i++)
  72.         if (mt[i] != -1) c++;
  73.     res = c * a + (empty - 2 * c) * b;
  74.     printf("%d\n", res);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement