QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618290 | #5114. Cells Coloring | rxzfn639 | AC ✓ | 421ms | 9460kb | C++23 | 2.2kb | 2024-10-06 20:29:54 | 2024-10-06 20:29:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAXN = 65050;
struct edg{ int pos, cap, rev; };
struct Dinic{
vector <edg> gph[MAXN];
int dis[MAXN], pnt[MAXN], flow;
void clear(){ for (int i = 0; i < MAXN; i++) gph[i].clear(); }
pii add_edge(int s, int e, int x){
gph[s].push_back({e, x, (int) gph[e].size()});
gph[e].push_back({s, 0, (int) gph[s].size() - 1});
return make_pair(s, gph[s].size() - 1);
}
bool bfs(int src, int sink){
memset(dis, 0, sizeof dis);
memset(pnt, 0, sizeof pnt);
queue <int> que;
que.push(src), dis[src] = 1;
while (que.size()){
int x = que.front(); que.pop();
for (auto &e : gph[x]){
if (e.cap > 0 && !dis[e.pos]){
dis[e.pos] = dis[x] + 1;
que.push(e.pos);
}
}
}
return dis[sink] > 0;
}
int dfs(int x, int sink, int f){
if (x == sink) return f;
for (; pnt[x] < gph[x].size(); pnt[x]++){
edg e = gph[x][pnt[x]];
if (e.cap > 0 && dis[e.pos] == dis[x] + 1){
int w = dfs(e.pos, sink, min(f, e.cap));
if (w){
gph[x][pnt[x]].cap -= w;
gph[e.pos][e.rev].cap += w;
return w;
}
}
}
return 0;
}
int max_flow(int src, int sink){
while (bfs(src, sink)){
int r;
while ((r = dfs(src, sink, 2e9))) flow += r;
}
return flow;
}
} G;
vector <pii> evec;
string mp[550];
int n, m, c, d, S, T, tot;
int main(){
ios :: sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> c >> d;
for (int i = 1; i <= n; i++)
cin >> mp[i];
S = 1, T = 2;
for (int i = 1; i <= n; i++)
evec.push_back(G.add_edge(S, 2 + i, 1));
for (int i = 1; i <= m; i++)
evec.push_back(G.add_edge(2 + n + i, T, 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
int id = (i - 1) * m + j;
if (mp[i][j - 1] == '*') continue;
tot++;
G.add_edge(2 + i, 2 + n + m + id, 1);
G.add_edge(2 + n + m + id, 2 + n + j, 1);
}
long long ret = 1ll * tot * d;
for (int k = 1; k <= max(n, m); k++){
int z = tot - G.max_flow(S, T);
ret = min(ret, 1ll * c * k + 1ll * d * z);
for (auto e : evec) G.gph[e.first][e.second].cap += 1;
}
cout << ret << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5596kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5596kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 137ms
memory: 7388kb
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...
output:
109972100048
result:
ok 1 number(s): "109972100048"
Test #4:
score: 0
Accepted
time: 180ms
memory: 7724kb
input:
250 250 62722280 506434 *.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..* .*..********..*...*..****...
output:
8437726048
result:
ok 1 number(s): "8437726048"
Test #5:
score: 0
Accepted
time: 361ms
memory: 9104kb
input:
250 250 85956327 344333 ..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..** .........*......*.*.........
output:
18268031127
result:
ok 1 number(s): "18268031127"
Test #6:
score: 0
Accepted
time: 375ms
memory: 9096kb
input:
250 250 768323813 489146 ...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*...... ...................*.......
output:
25999088192
result:
ok 1 number(s): "25999088192"
Test #7:
score: 0
Accepted
time: 135ms
memory: 7416kb
input:
250 250 865365220 7248935 .....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*. *..*.**.*...****.*.**.*...
output:
97440874100
result:
ok 1 number(s): "97440874100"
Test #8:
score: 0
Accepted
time: 29ms
memory: 6776kb
input:
153 225 485767021 308782855 .*.**.***..***..***..****.*****.***.....*.***.****.*.*......**......****.****.**.******...**...*.***.*..**.*****.****....*.*.*...**..****.**.******....*....****....**.*.*****.**.**.**.***...*.**.*.**.****.*.*....*.*****...*** *.*....*...*.*..*.*****.***.***.***.***..*.***...
output:
54405906352
result:
ok 1 number(s): "54405906352"
Test #9:
score: 0
Accepted
time: 0ms
memory: 5696kb
input:
17 20 823772868 410753944 .....*......**...... .......*............ ...............*.... ......*............. ...................* *........*.*..*..... .....*.............* ..*..........*.*.... .......*............ ...**...........**.* .................... **......**.......*.. .*.............*.... ....
output:
16062438436
result:
ok 1 number(s): "16062438436"
Test #10:
score: 0
Accepted
time: 49ms
memory: 6680kb
input:
227 129 426009970 614728889 *..****..*..*.********.*.*..***.*.*..**..*.***.**.**.***..*.***..*..*.***.*.*.**..*****.**..***.*.****.**.***.****..**.**.*.**.** *...*****.**....****..*....*.*.**.*****.*..*****...*...**...******..****.*..**...***.*.*.*..*.*.*..*.******.*****..*.**********.* .*.*..**..**...
output:
49843166490
result:
ok 1 number(s): "49843166490"
Test #11:
score: 0
Accepted
time: 66ms
memory: 7416kb
input:
170 219 28214303 818736602 *..*....*..*..*....**.*...*..*.**....**.*..*........*.**.....*....*.*..*..*.**.....*..***......*.....*...*........**.*.*.***...*........**..**....***...**....*.*..........**....*....**.***....*.*.*..*..***.....*.*..***. .*.*.....**...*..*....*.*....*.*.**.........*...*..*....
output:
4514288480
result:
ok 1 number(s): "4514288480"
Test #12:
score: 0
Accepted
time: 4ms
memory: 5672kb
input:
221 2 186883458 764869506 *. .* ** .* ** ** *. .* .* .* *. .* .. ** ** *. .. .* .* ** ** *. .* *. .. *. ** .* ** .. ** .. ** .. .* *. ** .* .* ** .. .* ** ** ** ** .* .. .* .* .. ** .* ** .* ** .. ** *. ** .. .* *. .* ** .* ** .. .* ** .* ** ** .* ** ** .* ** ** .. .. .* ** .. ** .* *. *. *. *. *. *...
output:
17753928510
result:
ok 1 number(s): "17753928510"
Test #13:
score: 0
Accepted
time: 2ms
memory: 5840kb
input:
28 2 127093639 70848307 .* .* ** .. .. .* ** *. ** .. ** *. *. ** *. .. .* .* *. ** ** .* ** .. *. .* ** **
output:
1468878336
result:
ok 1 number(s): "1468878336"
Test #14:
score: 0
Accepted
time: 3ms
memory: 5668kb
input:
142 1 465099485 99077573 * . . * * * * * . * . * . * . . . . * * * * * . * . . * * * . * . . . . . * * * * * * * * . * . * . . * * . * * * * . . * * . * * * * * * * . . . * * * * * * . * . * . * * * * . . . * * . * . . * . * . . . * . . * . . . . * . * . * * * * * * * . * * * * . * . . . . * . * * ....
output:
5845576807
result:
ok 1 number(s): "5845576807"
Test #15:
score: 0
Accepted
time: 178ms
memory: 7764kb
input:
250 250 420456041 0 ...*****.....****......*.**..*..*.**.**...*.***..**.*......*.*.....**..*..**.*..***.*.****.*.**.*.....**..*.*.**....**..***......*...***..*...****.*****.*.***.**.*.*...****.***..*...*.*.**.***..*.***.*.****.*.*...**....***....*.*.**....***...*.***... *.**...**.**...*..*..*.*.*.**...
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 388ms
memory: 9076kb
input:
250 250 0 577876919 .............**.*.....*.....*.*..*.......*...*.................**........*........*....*...*.......*...*........................*.......*.....*.*.*.......*........................*...............*..*.*....*.*.*..................*..................... ...*...*...................*....
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 0ms
memory: 5924kb
input:
250 250 708109405 398540228 ********************************************************************************************************************************************************************************************************************************************************** *********************...
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 421ms
memory: 9452kb
input:
250 250 1000000000 1000000 .......................................................................................................................................................................................................................................................... .........................
output:
62500000000
result:
ok 1 number(s): "62500000000"
Test #19:
score: 0
Accepted
time: 415ms
memory: 9460kb
input:
250 250 1000000000 10000000 .......................................................................................................................................................................................................................................................... ........................
output:
250000000000
result:
ok 1 number(s): "250000000000"