QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#634465 | #5114. Cells Coloring | xzin | RE | 0ms | 0kb | C++14 | 2.5kb | 2024-10-12 17:23:30 | 2024-10-12 17:23:30 |
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;
system("pause");
return 0;
}
詳細信息
Test #1:
score: 0
Dangerous Syscalls
input:
3 4 2 1 .*** *..* **..
output:
4