QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344923 | #3810. Landscaping | PetroTarnavskyi# | AC ✓ | 3ms | 4400kb | C++20 | 2.6kb | 2024-03-05 18:59:59 | 2024-03-05 18:59:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const LL LINF = 1e18;
struct Graph
{
struct Edge
{
int from, to;
LL cap, flow;
};
int n;
vector<Edge> edges;
vector<VI> g;
VI d, p;
void init(int _n)
{
n = _n;
edges.clear();
g.clear();
g.resize(n);
d.resize(n);
p.resize(n);
}
void addEdge(int from, int to, LL cap)
{
assert(0 <= from && from < n);
assert(0 <= to && to < n);
assert(0 <= cap);
g[from].PB(SZ(edges));
edges.PB({from, to, cap, 0});
g[to].PB(SZ(edges));
edges.PB({to, from, 0, 0});
}
int bfs(int s, int t)
{
fill(ALL(d), -1);
d[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty())
{
int v = q.front();
q.pop();
for (int e : g[v])
{
int to = edges[e].to;
if (edges[e].flow < edges[e].cap && d[to] == -1)
{
d[to] = d[v] + 1;
q.push(to);
}
}
}
return d[t];
}
LL dfs(int v, int t, LL flow)
{
if (v == t || flow == 0)
return flow;
for (; p[v] < SZ(g[v]); p[v]++)
{
int e = g[v][p[v]], to = edges[e].to;
LL c = edges[e].cap, f = edges[e].flow;
if (f < c && (to == t || d[to] == d[v] + 1))
{
LL push = dfs(to, t, min(flow, c - f));
if (push > 0)
{
edges[e].flow += push;
edges[e ^ 1].flow -= push;
return push;
}
}
}
return 0;
}
LL flow(int s, int t)
{
assert(0 <= s && s < n);
assert(0 <= t && t < n);
assert(s != t);
LL flow = 0;
while (bfs(s, t) != -1)
{
fill(ALL(p), 0);
while (true)
{
LL f = dfs(s, t, LINF);
if (f == 0)
break;
flow += f;
}
}
return flow;
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a, b;
cin >> a >> b;
vector<string> v(n);
FOR (i, 0, n)
{
cin >> v[i];
}
int s = n * m;
int t = s + 1;
Graph g;
g.init(n * m + 2);
FOR (i, 0, n)
{
FOR (j, 0, m)
{
int idx = i * m + j;
if (v[i][j] == '.')
g.addEdge(s, idx, b);
else
g.addEdge(idx, t, b);
if (i + 1 < n)
{
g.addEdge(idx, idx + m, a);
g.addEdge(idx + m, idx, a);
}
if (j + 1 < m)
{
g.addEdge(idx, idx + 1, a);
g.addEdge(idx + 1, idx, a);
}
}
}
cout << g.flow(s, t) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
5 4 1000 2000 ...# #..# ...# ##.. ###.
output:
11000
result:
ok single line: '11000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4180kb
input:
50 50 1 1 ####################.###########################.# #.####..#.###########.############################ ########################################.#.####### ##.##..#####.######.#..############.########.##### #########.########.################.###########.## ###############.###.###########.###...
output:
237
result:
ok single line: '237'
Test #3:
score: 0
Accepted
time: 0ms
memory: 4256kb
input:
50 50 1 2 ....#.........##............#........#.........#.# ...........##..........##...............#....#...# ..##...............#....##...............#.......# ..............#...#....#....#..........#.......#.. #.##...................#......#................... ..........#...................#.......
output:
476
result:
ok single line: '476'
Test #4:
score: 0
Accepted
time: 2ms
memory: 4340kb
input:
50 50 1 1 ....#.........##............#........#.........#.# ...........##..........##...............#....#...# ..##...............#....##...............#.......# ..............#...#....#....#..........#.......#.. #.##...................#......#................... ..........#...................#.......
output:
239
result:
ok single line: '239'
Test #5:
score: 0
Accepted
time: 0ms
memory: 4208kb
input:
50 50 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###...
output:
121500000
result:
ok single line: '121500000'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4340kb
input:
50 50 100000 100000 ################################################## #.################################################ #.################################################ #.################################################ #.################################################ #.#######################...
output:
9800000
result:
ok single line: '9800000'
Test #7:
score: 0
Accepted
time: 0ms
memory: 4272kb
input:
50 50 100000 100000 ################################################## ################################################## ################################################## ################################################## ################################################## #########################...
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3848kb
input:
1 1 1 1 .
output:
0
result:
ok single line: '0'
Test #9:
score: 0
Accepted
time: 0ms
memory: 4232kb
input:
49 50 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###...
output:
119100000
result:
ok single line: '119100000'
Test #10:
score: 0
Accepted
time: 0ms
memory: 4400kb
input:
50 49 100000 100000 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##. ##..##......###.#.####..###.##.#..##..#.#####.#.# ##..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..# #####..#....###..#.#.#.#..##...##.##.#.##.#####.# #########..##.#.#.#....#.###.##..##..#...##.#...# .#..###..###.#.###.#..##...###...
output:
119000000
result:
ok single line: '119000000'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
50 1 2 1 # . # # # . . # . # . # . # # # # # . # . . . # . . . . # . # # # . # # . # . # # # . # . . # # . #
output:
20
result:
ok single line: '20'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
4 4 1000 1500 .... .#.. .#.. ####
output:
7000
result:
ok single line: '7000'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
1 50 2 1 ...#.#.##.#....#.####.##.#.########..#...##..#..##
output:
19
result:
ok single line: '19'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3508kb
input:
5 5 1 10 .#.#. #.#.# .#.#. #.#.# .#.#.
output:
40
result:
ok single line: '40'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
5 5 10 1 .#.#. #.#.# .#.#. #.#.# .#.#.
output:
12
result:
ok single line: '12'
Test #16:
score: 0
Accepted
time: 1ms
memory: 4240kb
input:
50 50 1 10 #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...
output:
4900
result:
ok single line: '4900'
Test #17:
score: 0
Accepted
time: 2ms
memory: 4400kb
input:
50 50 10 1 #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...
output:
1250
result:
ok single line: '1250'
Test #18:
score: 0
Accepted
time: 3ms
memory: 4220kb
input:
50 50 1 2 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###.#...#.#.....
output:
2099
result:
ok single line: '2099'
Test #19:
score: 0
Accepted
time: 3ms
memory: 4208kb
input:
50 50 1 1 #.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.# #..##......###.#.####..###.##.#..##..#.#####.#.### ..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#### ##..#....###..#.#.#.#..##...##.##.#.##.#####.##### #####..##.#.#.#....#.###.##..##..#...##.#...#.#..# ##..###.#.###.#..##...###.#...#.#.....
output:
1215
result:
ok single line: '1215'
Test #20:
score: 0
Accepted
time: 2ms
memory: 4268kb
input:
50 50 1 2 ####################.###########################.# #.####..#.###########.############################ ########################################.#.####### ##.##..#####.######.#..############.########.##### #########.########.################.###########.## ###############.###.###########.###...
output:
472
result:
ok single line: '472'