Advertisement
ivnikkk

Untitled

Jun 21st, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #define debug(tl) cerr<<#tl<<' '<<tl<<'\n';
  2. #include "bits/stdc++.h"
  3. using namespace std;
  4. #define all(mask) mask.begin(), mask.end()
  5. typedef long long ll;
  6. typedef pair<ll, ll> pll;
  7. typedef pair<int, int> pii;
  8. typedef long double ld;
  9. const int MAXN = 1e4;
  10. vector<int> gr[MAXN];
  11. vector<int> used(MAXN), mpair(MAXN, -1);
  12. bool kuhn(int v) {
  13.     if (used[v]) return false;
  14.     used[v] = true;
  15.     for (auto u : gr[v]) {
  16.         if (mpair[u] == -1 || kuhn(mpair[u])) {
  17.             mpair[u] = v;
  18.             return true;
  19.         }
  20.     }
  21.     return false;
  22. }
  23. signed main() {
  24.     ios_base::sync_with_stdio(false);
  25.     cin.tie(nullptr);
  26.     cout.tie(nullptr);
  27.     freopen("dominoes.in", "r", stdin);
  28.     freopen("dominoes.out", "w", stdout);
  29.     int n, m, a0, b0, cur = 0;
  30.     cin >> n >> m >> a0 >> b0;
  31.     vector<vector<char>> a(n + 1, vector<char>(m + 1, '.'));
  32.     for (int i = 0; i < n; i++) {
  33.         for (int j = 0; j < m; j++) {
  34.             cin >> a[i][j];
  35.             cur += a[i][j] == '*';
  36.         }
  37.     }
  38.     for (int i = 0; i < n; i++) {
  39.         for (int j = 0; j < m; j++) {
  40.             if (a[i][j] == '*') {
  41.                 if ((i + j) % 2) {
  42.                     if (a[i][j + 1] == '*') {
  43.                         gr[i * m + j].push_back(i * m + 1 + j);
  44.                     }
  45.                     if (a[i + 1][j] == '*') {
  46.                         gr[i * m + j].push_back(i * m + m + j);
  47.                     }
  48.                 }
  49.                 else {
  50.                     if (a[i][j + 1] == '*') {
  51.                         gr[i * m + j + 1].push_back(i * m + j);
  52.                     }
  53.                     if (a[i + 1][j] == '*') {
  54.                         gr[(i + 1) * m + j].push_back(i * m + j);
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     for (int i = 0; i < MAXN; i++) {
  61.         used.assign(MAXN, 0);
  62.         kuhn(i);
  63.     }
  64.     int cnt = 0;
  65.     for (int i = 0; i < MAXN; i++) {
  66.         if (mpair[i] != -1) {
  67.             cnt++;
  68.         }
  69.     }
  70.     cout << min(cnt * a0 + cur * b0 - cnt * 2 * b0, b0 * cur);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement