QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217458 | #5114. Cells Coloring | ucup-team1001 | AC ✓ | 149ms | 5816kb | C++20 | 3.2kb | 2023-10-16 21:19:08 | 2023-10-16 21:19:08 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define irep(i,l,r) for(int i = l; i <= r; ++i)
const int itinf = 1e9;
inline char rc(){
char ch = getchar();
while(1){
if(ch == '*' || ch == '.')return ch;
ch = getchar();
}
}
inline ll read(){
char ch = getchar();
ll s = 0; bool w = 0;
while(!isdigit(ch)){if(ch == '-')w = 1;ch = getchar();}
while(isdigit(ch))s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return w ? - s : s;
}
template<class T>
struct Flow {
const int n;
struct Edge {
int to;
T cap;
Edge(int to, T cap) : to(to), cap(cap) {}
};
std::vector<Edge> e;
std::vector<std::vector<int>> g;
std::vector<int> cur, h;
Flow(int n) : n(n), g(n) {}
bool bfs(int s, int t) {
h.assign(n, -1);
std::queue<int> que;
h[s] = 0;
que.push(s);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (int i : g[u]) {
auto [v, c] = e[i];
if (c > 0 && h[v] == -1) {
h[v] = h[u] + 1;
if (v == t) {
return true;
}
que.push(v);
}
}
}
return false;
}
T dfs(int u, int t, T f) {
if (u == t) {
return f;
}
auto r = f;
for (int &i = cur[u]; i < int(g[u].size()); ++i) {
const int j = g[u][i];
auto [v, c] = e[j];
if (c > 0 && h[v] == h[u] + 1) {
auto a = dfs(v, t, std::min(r, c));
e[j].cap -= a;
e[j ^ 1].cap += a;
r -= a;
if (r == 0) {
return f;
}
}
}
return f - r;
}
void addEdge(int u, int v, T c) {
// cout << e.size() << ' ' << u << ' ' << v << ' ' << c << endl;
g[u].push_back(e.size());
e.emplace_back(v, c);
g[v].push_back(e.size());
e.emplace_back(u, 0);
}
T maxFlow(int s, int t) {
T ans = 0;
while (bfs(s, t)) {
cur.assign(n, 0);
ans += dfs(s, t, std::numeric_limits<T>::max());
}
return ans;
}
};
int main(){
int n = read(), m = read(), cnt = 0;
ll c = read(), d = read();
Flow<ll>flow(n + m + 2);
int S = n + m, T = n + m + 1;
vector<array<int, 2>>eg;
irep(i, 0, n - 1){
flow.addEdge(S, i, 1);
}
irep(i, 0, m - 1){
flow.addEdge(i + n, T, 1);
}
irep(i, 0, n - 1){
irep(j, 0, m - 1){
if(rc() == '.'){
++ cnt;
//eg.push_back({i, j + n});
flow.addEdge(i,j + n,1);
}
}
}
// return 0;
ll ans = d * cnt;//std::numeric_limits<ll>::max();
irep(k, 1, max(n, m)){
cnt -= flow.maxFlow(S,T);
ans = min(ans ,1ll * (cnt) * d + 1ll * k * c);
// for(auto [u, v] : eg)flow.addEdge(u, v, 1);
// cout << flow.maxFlow(S,T) << endl;
for(int i = 0; i < (n + m); ++ i){
flow.e[i * 2].cap ++;
// cerr << flow.e[i * 2].to << ' ' << flow.e[i * 2].cap << endl;
}
}
printf("%lld", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3736kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 53ms
memory: 4480kb
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...
output:
109972100048
result:
ok 1 number(s): "109972100048"
Test #4:
score: 0
Accepted
time: 66ms
memory: 4544kb
input:
250 250 62722280 506434 *.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..* .*..********..*...*..****...
output:
8437726048
result:
ok 1 number(s): "8437726048"
Test #5:
score: 0
Accepted
time: 130ms
memory: 5628kb
input:
250 250 85956327 344333 ..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..** .........*......*.*.........
output:
18268031127
result:
ok 1 number(s): "18268031127"
Test #6:
score: 0
Accepted
time: 138ms
memory: 5452kb
input:
250 250 768323813 489146 ...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*...... ...................*.......
output:
25999088192
result:
ok 1 number(s): "25999088192"
Test #7:
score: 0
Accepted
time: 51ms
memory: 4420kb
input:
250 250 865365220 7248935 .....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*. *..*.**.*...****.*.**.*...
output:
97440874100
result:
ok 1 number(s): "97440874100"
Test #8:
score: 0
Accepted
time: 7ms
memory: 3980kb
input:
153 225 485767021 308782855 .*.**.***..***..***..****.*****.***.....*.***.****.*.*......**......****.****.**.******...**...*.***.*..**.*****.****....*.*.*...**..****.**.******....*....****....**.*.*****.**.**.**.***...*.**.*.**.****.*.*....*.*****...*** *.*....*...*.*..*.*****.***.***.***.***..*.***...
output:
54405906352
result:
ok 1 number(s): "54405906352"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3768kb
input:
17 20 823772868 410753944 .....*......**...... .......*............ ...............*.... ......*............. ...................* *........*.*..*..... .....*.............* ..*..........*.*.... .......*............ ...**...........**.* .................... **......**.......*.. .*.............*.... ....
output:
16062438436
result:
ok 1 number(s): "16062438436"
Test #10:
score: 0
Accepted
time: 19ms
memory: 3900kb
input:
227 129 426009970 614728889 *..****..*..*.********.*.*..***.*.*..**..*.***.**.**.***..*.***..*..*.***.*.*.**..*****.**..***.*.****.**.***.****..**.**.*.**.** *...*****.**....****..*....*.*.**.*****.*..*****...*...**...******..****.*..**...***.*.*.*..*.*.*..*.******.*****..*.**********.* .*.*..**..**...
output:
49843166490
result:
ok 1 number(s): "49843166490"
Test #11:
score: 0
Accepted
time: 18ms
memory: 4320kb
input:
170 219 28214303 818736602 *..*....*..*..*....**.*...*..*.**....**.*..*........*.**.....*....*.*..*..*.**.....*..***......*.....*...*........**.*.*.***...*........**..**....***...**....*.*..........**....*....**.***....*.*.*..*..***.....*.*..***. .*.*.....**...*..*....*.*....*.*.**.........*...*..*....
output:
4514288480
result:
ok 1 number(s): "4514288480"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3784kb
input:
221 2 186883458 764869506 *. .* ** .* ** ** *. .* .* .* *. .* .. ** ** *. .. .* .* ** ** *. .* *. .. *. ** .* ** .. ** .. ** .. .* *. ** .* .* ** .. .* ** ** ** ** .* .. .* .* .. ** .* ** .* ** .. ** *. ** .. .* *. .* ** .* ** .. .* ** .* ** ** .* ** ** .* ** ** .. .. .* ** .. ** .* *. *. *. *. *. *...
output:
17753928510
result:
ok 1 number(s): "17753928510"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
28 2 127093639 70848307 .* .* ** .. .. .* ** *. ** .. ** *. *. ** *. .. .* .* *. ** ** .* ** .. *. .* ** **
output:
1468878336
result:
ok 1 number(s): "1468878336"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3824kb
input:
142 1 465099485 99077573 * . . * * * * * . * . * . * . . . . * * * * * . * . . * * * . * . . . . . * * * * * * * * . * . * . . * * . * * * * . . * * . * * * * * * * . . . * * * * * * . * . * . * * * * . . . * * . * . . * . * . . . * . . * . . . . * . * . * * * * * * * . * * * * . * . . . . * . * * ....
output:
5845576807
result:
ok 1 number(s): "5845576807"
Test #15:
score: 0
Accepted
time: 64ms
memory: 4532kb
input:
250 250 420456041 0 ...*****.....****......*.**..*..*.**.**...*.***..**.*......*.*.....**..*..**.*..***.*.****.*.**.*.....**..*.*.**....**..***......*...***..*...****.*****.*.***.**.*.*...****.***..*...*.*.**.***..*.***.*.****.*.*...**....***....*.*.**....***...*.***... *.**...**.**...*..*..*.*.*.**...
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 144ms
memory: 5456kb
input:
250 250 0 577876919 .............**.*.....*.....*.*..*.......*...*.................**........*........*....*...*.......*...*........................*.......*.....*.*.*.......*........................*...............*..*.*....*.*.*..................*..................... ...*...*...................*....
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 1ms
memory: 4076kb
input:
250 250 708109405 398540228 ********************************************************************************************************************************************************************************************************************************************************** *********************...
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 146ms
memory: 5816kb
input:
250 250 1000000000 1000000 .......................................................................................................................................................................................................................................................... .........................
output:
62500000000
result:
ok 1 number(s): "62500000000"
Test #19:
score: 0
Accepted
time: 149ms
memory: 5712kb
input:
250 250 1000000000 10000000 .......................................................................................................................................................................................................................................................... ........................
output:
250000000000
result:
ok 1 number(s): "250000000000"