QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#634466 | #5114. Cells Coloring | xzin | RE | 0ms | 3848kb | C++14 | 2.5kb | 2024-10-12 17:23:38 | 2024-10-12 17:23:39 |
Judging History
answer
#include <bits/stdc++.h>
#define N 600
#define INF 1e9
#define ll long long
using namespace std;
struct MF {
struct edge {
int v, nxt, cap, flow;
} e[N];
int fir[N], cnt = 0;
int n, S, T;
ll maxflow = 0;
int dep[N], cur[N];
void init(int nn, int s, int t) {
memset(fir, -1, sizeof fir);
cnt = 0; maxflow = 0;
n = nn, S = s, T = t;
}
void addedge(int u, int v, int w) {
e[cnt] = {v, fir[u], w, 0};
fir[u] = cnt++;
e[cnt] = {u, fir[v], 0, 0};
fir[v] = cnt++;
}
bool bfs() {
queue<int> q;
memset(dep, 0, sizeof(int) * (n + 1));
dep[S] = 1;
q.push(S);
while (q.size()) {
int u = q.front();
q.pop();
for (int i = fir[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if ((!dep[v]) && (e[i].cap > e[i].flow)) {
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
return dep[T];
}
int dfs(int u, int flow) {
if ((u == T) || (!flow)) return flow;
int ret = 0;
for (int& i = cur[u]; ~i; i = e[i].nxt) {
int v = e[i].v, d;
if ((dep[v] == dep[u] + 1) &&
(d = dfs(v, min(flow - ret, e[i].cap - e[i].flow)))) {
ret += d;
e[i].flow += d;
e[i ^ 1].flow -= d;
if (ret == flow) return ret;
}
}
return ret;
}
void dinic() {
while (bfs()) {
memcpy(cur, fir, sizeof(int) * (n + 1));
maxflow += dfs(S, INF);
}
}
} mf;
int n, m;
long long c, d, ans = 1e15, sum, num;
string s[N];
int h[N], l[N];
void work(int k) {
mf.init(n + m + 2, 0, n + m + 1);
for(int i = 1;i <= n; i++) {
if(h[i] > k)
for(int j = 1; j <= m; j++)
if(l[i] > k) mf.addedge(i, n + j, 1);
}
for(int i = 1;i <= n; i++)
if(h[i] > k) mf.addedge(0, i, h[i] - k), num += h[i] - k;
for(int i = 1;i <= m; i++)
if(l[i] > k) mf.addedge(n + i, n + m + 1, l[i] - k), num += l[i] - k;
mf.dinic();
num = num - mf.maxflow;
sum = c * k + d * num;
}
int main() {
cin>>n>>m>>c>>d;
for(int i = 1;i <= n; i++) {
cin>>s[i]; s[i] = " " + s[i];
for(int j = 1; j <= m; j++) {
if(s[i][j] == '.') h[i]++;
}
}
for(int i = 1;i <= m; i++)
for(int j = 1; j <= n; j++)
if(s[j][i] == '.') l[i]++;
for(int i = 0;i <= max(n, m); i++) {
sum = 0; num = 0; work(i);
ans = min(ans, sum);
}
cout<<ans<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3848kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Runtime Error
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...