QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#439504#3810. Landscapinghos_lyricAC ✓2ms4236kbC++144.0kb2024-06-12 05:35:572024-06-12 05:35:58

Judging History

你现在查看的是最新测评结果

  • [2024-06-12 05:35:58]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:4236kb
  • [2024-06-12 05:35:57]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class Flow> struct MaxFlow {
  // Watch out when using types other than int and long long.
  static constexpr Flow FLOW_EPS = 1e-10L;
  static constexpr Flow FLOW_INF = std::numeric_limits<Flow>::max();
  
  int n, m;
  vector<int> ptr, nxt, zu;
  vector<Flow> capa;

  explicit MaxFlow(int n_) : n(n_), m(0), ptr(n_, -1) {}
  void ae(int u, int v, Flow w0, Flow w1 = 0) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(0 <= w0);
    assert(0 <= w1);
    nxt.push_back(ptr[u]); zu.push_back(v); capa.push_back(w0); ptr[u] = m++;
    nxt.push_back(ptr[v]); zu.push_back(u); capa.push_back(w1); ptr[v] = m++;
  }

  vector<int> see, lev, que;

  Flow augment(int u, int t, Flow limFlow) {
    if (u == t) return limFlow;
    for (int &i = see[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
      const int v = zu[i];
      if (lev[u] < lev[v]) {
        const Flow f = augment(v, t, min(limFlow, capa[i]));
        if (f > FLOW_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
    }
    return 0;
  }
  bool bfs(int s, int t) {
    for (int u = 0; u < n; ++u) { see[u] = ptr[u]; lev[u] = -1; }
    auto head = que.begin(), tail = que.begin();
    for (lev[*tail++ = s] = 0; head != tail; ) {
      const int u = *head++;
      for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
        const int v = zu[i];
        if (!~lev[v]) {
          lev[*tail++ = v] = lev[u] + 1;
          if (v == t) return true;
        }
      }
    }
    return false;
  }
  Flow run(int s, int t, Flow limFlow = FLOW_INF) {
    see.resize(n); lev.resize(n); que.resize(n);
    Flow flow = 0;
    for (; flow + FLOW_EPS < limFlow && bfs(s, t); ) {
      for (Flow f; (f = augment(s, t, limFlow - flow)) > FLOW_EPS; flow += f) {}
    }
    return flow;
  }
};

////////////////////////////////////////////////////////////////////////////////


/*
  problem
    change '.' -> '#": cost B
    change '#' -> '.': cost B
    adjacent '.' and '#': cost A
*/

int M, N;
Int A, B;
char C[60][60];

int main() {
  for (; ~scanf("%d%d%lld%lld", &M, &N, &A, &B); ) {
    for (int x = 0; x < M; ++x) {
      scanf("%s", C[x]);
    }
    
    MaxFlow<Int> mf(M * N + 2);
    const int src = M * N;
    const int snk = M * N + 1;
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
      if (C[x][y] == '.') {
        mf.ae(src, x * N + y, B);
      } else if (C[x][y] == '#') {
        mf.ae(x * N + y, snk, B);
      } else {
        assert(false);
      }
    }
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
      if (x + 1 < M) mf.ae(x * N + y, (x + 1) * N + y, A, A);
      if (y + 1 < N) mf.ae(x * N + y, x * N + (y + 1), A, A);
    }
    const Int ans = mf.run(src, snk);
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3764kb

input:

5 4 1000 2000
...#
#..#
...#
##..
###.

output:

11000

result:

ok single line: '11000'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3956kb

input:

50 50 1 1
####################.###########################.#
#.####..#.###########.############################
########################################.#.#######
##.##..#####.######.#..############.########.#####
#########.########.################.###########.##
###############.###.###########.###...

output:

237

result:

ok single line: '237'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3944kb

input:

50 50 1 2
....#.........##............#........#.........#.#
...........##..........##...............#....#...#
..##...............#....##...............#.......#
..............#...#....#....#..........#.......#..
#.##...................#......#...................
..........#...................#.......

output:

476

result:

ok single line: '476'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

50 50 1 1
....#.........##............#........#.........#.#
...........##..........##...............#....#...#
..##...............#....##...............#.......#
..............#...#....#....#..........#.......#..
#.##...................#......#...................
..........#...................#.......

output:

239

result:

ok single line: '239'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3952kb

input:

50 50 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###...

output:

121500000

result:

ok single line: '121500000'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3976kb

input:

50 50 100000 100000
##################################################
#.################################################
#.################################################
#.################################################
#.################################################
#.#######################...

output:

9800000

result:

ok single line: '9800000'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

50 50 100000 100000
##################################################
##################################################
##################################################
##################################################
##################################################
#########################...

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

1 1 1 1
.


output:

0

result:

ok single line: '0'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3976kb

input:

49 50 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###...

output:

119100000

result:

ok single line: '119100000'

Test #10:

score: 0
Accepted
time: 2ms
memory: 4236kb

input:

50 49 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.
##..##......###.#.####..###.##.#..##..#.#####.#.#
##..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#
#####..#....###..#.#.#.#..##...##.##.#.##.#####.#
#########..##.#.#.#....#.###.##..##..#...##.#...#
.#..###..###.#.###.#..##...###...

output:

119000000

result:

ok single line: '119000000'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3780kb

input:

50 1 2 1
#
.
#
#
#
.
.
#
.
#
.
#
.
#
#
#
#
#
.
#
.
.
.
#
.
.
.
.
#
.
#
#
#
.
#
#
.
#
.
#
#
#
.
#
.
.
#
#
.
#

output:

20

result:

ok single line: '20'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

4 4 1000 1500
....
.#..
.#..
####


output:

7000

result:

ok single line: '7000'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

1 50 2 1
...#.#.##.#....#.####.##.#.########..#...##..#..##

output:

19

result:

ok single line: '19'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

5 5 1 10
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.


output:

40

result:

ok single line: '40'

Test #15:

score: 0
Accepted
time: 0ms
memory: 4080kb

input:

5 5 10 1
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.


output:

12

result:

ok single line: '12'

Test #16:

score: 0
Accepted
time: 0ms
memory: 4040kb

input:

50 50 1 10
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...

output:

4900

result:

ok single line: '4900'

Test #17:

score: 0
Accepted
time: 1ms
memory: 4032kb

input:

50 50 10 1
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...

output:

1250

result:

ok single line: '1250'

Test #18:

score: 0
Accepted
time: 0ms
memory: 4028kb

input:

50 50 1 2
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###.#...#.#.....

output:

2099

result:

ok single line: '2099'

Test #19:

score: 0
Accepted
time: 2ms
memory: 3948kb

input:

50 50 1 1
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###.#...#.#.....

output:

1215

result:

ok single line: '1215'

Test #20:

score: 0
Accepted
time: 1ms
memory: 4044kb

input:

50 50 1 2
####################.###########################.#
#.####..#.###########.############################
########################################.#.#######
##.##..#####.######.#..############.########.#####
#########.########.################.###########.##
###############.###.###########.###...

output:

472

result:

ok single line: '472'