QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#439105 | #3810. Landscaping | alpha1022 | AC ✓ | 4ms | 4680kb | C++14 | 2.0kb | 2024-06-11 16:11:55 | 2024-06-11 16:11:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<class Z>
struct MaxFlow {
static constexpr Z inf = numeric_limits<Z>::max();
int n;
vector<vector<tuple<int, int, Z>>> g;
MaxFlow(int n) : n(n), g(n) {}
int newNode() { g.emplace_back(); return n++; }
void addEdge(int u, int v, Z c) {
int ru = g[u].size(), rv = g[v].size();
g[u].emplace_back(v, rv, c), g[v].emplace_back(u, ru, 0);
}
vector<int> level, cur;
Z dinic(int s, int t) {
level.resize(n), cur.resize(n);
auto getLevel = [&]() {
queue<int> q; fill_n(level.begin(), n, -1), q.push(s), level[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto [v, _, c]: g[u])
if (c && level[v] == -1) level[v] = level[u] + 1, q.push(v);
}
};
function<Z(int, Z)> cap = [&](int u, Z lim) {
if (u == t) return lim;
Z ret = 0;
for (; cur[u] && ret < lim; --cur[u]) {
auto &[v, rev, c] = g[u][cur[u] - 1];
if (level[v] == level[u] + 1) {
Z flow = cap(v, min(lim - ret, c));
ret += flow, c -= flow, get<2>(g[v][rev]) += flow;
if (ret == lim) return ret;
}
}
return ret;
};
Z ret = 0;
while (getLevel(), level[t] != -1) {
for (int i = 0; i < n; ++i) cur[i] = g[i].size();
ret += cap(s, inf);
}
return ret;
}
};
int main() {
int n, m, a, b; cin >> n >> m >> a >> b;
MaxFlow<int> maxFlow(n * m + 2); int s = n * m, t = s + 1;
vector<string> str(n);
for (int i = 0; i < n; ++i) cin >> str[i];
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (str[i][j] == '.') maxFlow.addEdge(s, i * m + j, b);
else maxFlow.addEdge(i * m + j, t, b);
for (auto [dx, dy] : vector<pair<int, int>>{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}) {
int nx = i + dx, ny = j + dy;
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
maxFlow.addEdge(i * m + j, nx * m + ny, a);
}
}
printf("%d\n", maxFlow.dinic(s, t));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
5 4 1000 2000 ...# #..# ...# ##.. ###.
output:
11000
result:
ok single line: '11000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4392kb
input:
50 50 1 1 ####################.###########################.# #.####..#.###########.############################ ########################################.#.####### ##.##..#####.######.#..############.########.##### #########.########.################.###########.## ###############.###.###########.###...
output:
237
result:
ok single line: '237'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4680kb
input:
50 50 1 2 ....#.........##............#........#.........#.# ...........##..........##...............#....#...# ..##...............#....##...............#.......# ..............#...#....#....#..........#.......#.. #.##...................#......#................... ..........#...................#.......
output:
476
result:
ok single line: '476'
Test #4:
score: 0
Accepted
time: 0ms
memory: 4568kb
input:
50 50 1 1 ....#.........##............#........#.........#.# ...........##..........##...............#....#...# ..##...............#....##...............#.......# ..............#...#....#....#..........#.......#.. #.##...................#......#................... ..........#...................#.......
output:
239
result:
ok single line: '239'
Test #5:
score: 0
Accepted
time: 4ms
memory: 4416kb
input:
50 50 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###...
output:
121500000
result:
ok single line: '121500000'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4404kb
input:
50 50 100000 100000 ################################################## #.################################################ #.################################################ #.################################################ #.################################################ #.#######################...
output:
9800000
result:
ok single line: '9800000'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4316kb
input:
50 50 100000 100000 ################################################## ################################################## ################################################## ################################################## ################################################## #########################...
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3992kb
input:
1 1 1 1 .
output:
0
result:
ok single line: '0'
Test #9:
score: 0
Accepted
time: 4ms
memory: 4320kb
input:
49 50 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###...
output:
119100000
result:
ok single line: '119100000'
Test #10:
score: 0
Accepted
time: 4ms
memory: 4376kb
input:
50 49 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##. ##..##......###.#.####..###.##.#..##..#.#####.#.# ##..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..# #####..#....###..#.#.#.#..##...##.##.#.##.#####.# #########..##.#.#.#....#.###.##..##..#...##.#...# .#..###..###.#.###.#..##...###...
output:
119000000
result:
ok single line: '119000000'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3796kb
input:
50 1 2 1 # . # # # . . # . # . # . # # # # # . # . . . # . . . . # . # # # . # # . # . # # # . # . . # # . #
output:
20
result:
ok single line: '20'
Test #12:
score: 0
Accepted
time: 0ms
memory: 4072kb
input:
4 4 1000 1500 .... .#.. .#.. ####
output:
7000
result:
ok single line: '7000'
Test #13:
score: 0
Accepted
time: 0ms
memory: 4000kb
input:
1 50 2 1 ...#.#.##.#....#.####.##.#.########..#...##..#..##
output:
19
result:
ok single line: '19'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
5 5 1 10 .#.#. #.#.# .#.#. #.#.# .#.#.
output:
40
result:
ok single line: '40'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
5 5 10 1 .#.#. #.#.# .#.#. #.#.# .#.#.
output:
12
result:
ok single line: '12'
Test #16:
score: 0
Accepted
time: 0ms
memory: 4408kb
input:
50 50 1 10 #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...
output:
4900
result:
ok single line: '4900'
Test #17:
score: 0
Accepted
time: 1ms
memory: 4612kb
input:
50 50 10 1 #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...
output:
1250
result:
ok single line: '1250'
Test #18:
score: 0
Accepted
time: 4ms
memory: 4412kb
input:
50 50 1 2 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###.#...#.#.....
output:
2099
result:
ok single line: '2099'
Test #19:
score: 0
Accepted
time: 4ms
memory: 4324kb
input:
50 50 1 1 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###.#...#.#.....
output:
1215
result:
ok single line: '1215'
Test #20:
score: 0
Accepted
time: 1ms
memory: 4404kb
input:
50 50 1 2 ####################.###########################.# #.####..#.###########.############################ ########################################.#.####### ##.##..#####.######.#..############.########.##### #########.########.################.###########.## ###############.###.###########.###...
output:
472
result:
ok single line: '472'