QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#690659 | #5114. Cells Coloring | QFshengxiu | AC ✓ | 534ms | 13644kb | C++23 | 3.3kb | 2024-10-31 00:19:45 | 2024-10-31 00:19:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define QF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
constexpr int N = 1e5 + 10;
constexpr int M = 1e5 + 10;
const int inf = 1e9;
int n, m, s, t, tot = 1, ans;
int head[N], now[N], dep[N];
bool used[N];
string st[N];
struct edge
{
int v, w, nxt;
} edges[M << 1];
void add(int u, int v, int w)
{
edges[++tot] = {v, w, head[u]};
head[u] = tot;
}
void addedge(int u, int v, int w)
{
used[u] = true;
used[v] = true;
add(u, v, w);
add(v, u, 0);
}
bool bfs()
{
memset(dep, 0, sizeof(dep));
queue<int> q;
q.push(s);
dep[s] = 1;
while (!q.empty())
{
auto u = q.front();
q.pop();
for (int i = head[u]; i; i = edges[i].nxt)
{
auto [v, w, x] = edges[i];
if (w && !dep[v])
{
dep[v] = dep[u] + 1;
q.push(v);
if (v == t)
return true;
}
}
}
return false;
}
int dfs(int u, int flow)
{
if (u == t)
{
return flow;
}
int res = 0;
for (int i = now[u]; i && flow; i = edges[i].nxt)
{
now[u] = i;
auto [v, w, x] = edges[i];
if (w && dep[v] == dep[u] + 1)
{
int k = dfs(v, min(flow, w));
if (!k)
{
dep[v] = 0;
}
res += k;
flow -= k;
edges[i].w -= k;
edges[i ^ 1].w += k;
}
}
return res;
}
void clear()
{
tot = 1;
memset(head, 0, sizeof(head));
memset(used, 0, sizeof(used));
s = 0, t = 0;
}
void solve()
{
int c, d;
cin >> n >> m >> c >> d;
for (int i = 1; i <= n; i++)
{
cin >> st[i];
st[i] = " " + st[i];
}
s = 1e5, t = s + 1;
int all = 0;
for (int j = 1; j <= n; j++)
{
addedge(s, j, 1);
for (int i = 1; i <= m; i++)
{
if (st[j][i] != '*')
{
addedge(j, i + n, 1);
all++;
}
}
}
for (int j = 1; j <= m; j++)
{
addedge(j + n, t, 1);
}
int minn = d * all;
ans = 0;
while (bfs())
{
for (int i = 1; i <= n + m; i++)
{
now[i] = head[i];
}
now[s] = head[s];
now[t] = head[t];
ans += dfs(s, inf);
}
int nx = c + (all - ans) * d;
minn = min(minn, nx);
for (int i = 2; i <= max(n, m); i++)
{
for (int x = head[s]; x; x = edges[x].nxt)
{
edges[x].w++;
}
for (int x = head[t]; x; x = edges[x].nxt)
{
edges[x ^ 1].w++;
}
while (bfs())
{
for (int w = 1; w <= n + m; w++)
{
now[w] = head[w];
}
now[s] = head[s];
now[t] = head[t];
ans += dfs(s, inf);
}
// cout << ans << endl;
nx = i * c + (all - ans) * d;
minn = min(minn, nx);
}
cout << minn << endl;
}
signed main()
{
QF;
int T = 1;
for (int i = 1; i <= T; i++)
{
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 9696kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 7712kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 143ms
memory: 9832kb
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...
output:
109972100048
result:
ok 1 number(s): "109972100048"
Test #4:
score: 0
Accepted
time: 185ms
memory: 13008kb
input:
250 250 62722280 506434 *.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..* .*..********..*...*..****...
output:
8437726048
result:
ok 1 number(s): "8437726048"
Test #5:
score: 0
Accepted
time: 472ms
memory: 12328kb
input:
250 250 85956327 344333 ..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..** .........*......*.*.........
output:
18268031127
result:
ok 1 number(s): "18268031127"
Test #6:
score: 0
Accepted
time: 481ms
memory: 13232kb
input:
250 250 768323813 489146 ...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*...... ...................*.......
output:
25999088192
result:
ok 1 number(s): "25999088192"
Test #7:
score: 0
Accepted
time: 135ms
memory: 12760kb
input:
250 250 865365220 7248935 .....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*. *..*.**.*...****.*.**.*...
output:
97440874100
result:
ok 1 number(s): "97440874100"
Test #8:
score: 0
Accepted
time: 27ms
memory: 10392kb
input:
153 225 485767021 308782855 .*.**.***..***..***..****.*****.***.....*.***.****.*.*......**......****.****.**.******...**...*.***.*..**.*****.****....*.*.*...**..****.**.******....*....****....**.*.*****.**.**.**.***...*.**.*.**.****.*.*....*.*****...*** *.*....*...*.*..*.*****.***.***.***.***..*.***...
output:
54405906352
result:
ok 1 number(s): "54405906352"
Test #9:
score: 0
Accepted
time: 2ms
memory: 8104kb
input:
17 20 823772868 410753944 .....*......**...... .......*............ ...............*.... ......*............. ...................* *........*.*..*..... .....*.............* ..*..........*.*.... .......*............ ...**...........**.* .................... **......**.......*.. .*.............*.... ....
output:
16062438436
result:
ok 1 number(s): "16062438436"
Test #10:
score: 0
Accepted
time: 46ms
memory: 10344kb
input:
227 129 426009970 614728889 *..****..*..*.********.*.*..***.*.*..**..*.***.**.**.***..*.***..*..*.***.*.*.**..*****.**..***.*.****.**.***.****..**.**.*.**.** *...*****.**....****..*....*.*.**.*****.*..*****...*...**...******..****.*..**...***.*.*.*..*.*.*..*.******.*****..*.**********.* .*.*..**..**...
output:
49843166490
result:
ok 1 number(s): "49843166490"
Test #11:
score: 0
Accepted
time: 50ms
memory: 9608kb
input:
170 219 28214303 818736602 *..*....*..*..*....**.*...*..*.**....**.*..*........*.**.....*....*.*..*..*.**.....*..***......*.....*...*........**.*.*.***...*........**..**....***...**....*.*..........**....*....**.***....*.*.*..*..***.....*.*..***. .*.*.....**...*..*....*.*....*.*.**.........*...*..*....
output:
4514288480
result:
ok 1 number(s): "4514288480"
Test #12:
score: 0
Accepted
time: 6ms
memory: 9716kb
input:
221 2 186883458 764869506 *. .* ** .* ** ** *. .* .* .* *. .* .. ** ** *. .. .* .* ** ** *. .* *. .. *. ** .* ** .. ** .. ** .. .* *. ** .* .* ** .. .* ** ** ** ** .* .. .* .* .. ** .* ** .* ** .. ** *. ** .. .* *. .* ** .* ** .. .* ** .* ** ** .* ** ** .* ** ** .. .. .* ** .. ** .* *. *. *. *. *. *...
output:
17753928510
result:
ok 1 number(s): "17753928510"
Test #13:
score: 0
Accepted
time: 2ms
memory: 9804kb
input:
28 2 127093639 70848307 .* .* ** .. .. .* ** *. ** .. ** *. *. ** *. .. .* .* *. ** ** .* ** .. *. .* ** **
output:
1468878336
result:
ok 1 number(s): "1468878336"
Test #14:
score: 0
Accepted
time: 4ms
memory: 9764kb
input:
142 1 465099485 99077573 * . . * * * * * . * . * . * . . . . * * * * * . * . . * * * . * . . . . . * * * * * * * * . * . * . . * * . * * * * . . * * . * * * * * * * . . . * * * * * * . * . * . * * * * . . . * * . * . . * . * . . . * . . * . . . . * . * . * * * * * * * . * * * * . * . . . . * . * * ....
output:
5845576807
result:
ok 1 number(s): "5845576807"
Test #15:
score: 0
Accepted
time: 189ms
memory: 13332kb
input:
250 250 420456041 0 ...*****.....****......*.**..*..*.**.**...*.***..**.*......*.*.....**..*..**.*..***.*.****.*.**.*.....**..*.*.**....**..***......*...***..*...****.*****.*.***.**.*.*...****.***..*...*.*.**.***..*.***.*.****.*.*...**....***....*.*.**....***...*.***... *.**...**.**...*..*..*.*.*.**...
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 502ms
memory: 13324kb
input:
250 250 0 577876919 .............**.*.....*.....*.*..*.......*...*.................**........*........*....*...*.......*...*........................*.......*.....*.*.*.......*........................*...............*..*.*....*.*.*..................*..................... ...*...*...................*....
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 5ms
memory: 10088kb
input:
250 250 708109405 398540228 ********************************************************************************************************************************************************************************************************************************************************** *********************...
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 522ms
memory: 13452kb
input:
250 250 1000000000 1000000 .......................................................................................................................................................................................................................................................... .........................
output:
62500000000
result:
ok 1 number(s): "62500000000"
Test #19:
score: 0
Accepted
time: 534ms
memory: 13644kb
input:
250 250 1000000000 10000000 .......................................................................................................................................................................................................................................................... ........................
output:
250000000000
result:
ok 1 number(s): "250000000000"