QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#627393 | #5114. Cells Coloring | MENDAX | WA | 2ms | 8436kb | C++20 | 3.0kb | 2024-10-10 15:48:18 | 2024-10-10 15:48:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
const int N = 2e5 + 10;
//n*m+n+m
struct edge {
int cap;
}e[N];
vector <pii> G[N];
int n,m,s,t,tot = -1;
/*
int dis[N],cur[N];
bool vis[N];
bool BFS() {
memset(vis,0,sizeof vis);
queue <int> q;
q.push(s);
dis[s] = 0;vis[s] = 1;
while (!q.empty()) {
int x = q.front();q.pop();
for (auto ed : G[x]) {
int y = ed.first,num = ed.second;
if (!vis[y] && e[num].cap > e[num].flow) {
vis[y] = 1;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return vis[t];
}
int DFS(int x,int res) {
if (x == t || !res) return res;
int now = 0;
for (int &i = cur[x];i < G[x].size();++i) {
pii ed = G[x][i];
int y = ed.first,num = ed.second;
if (dis[x] + 1 == dis[y]){
int f = DFS(y,min(res,e[num].cap - e[num].flow));
if (f > 0) {
e[num].flow += f;
e[num ^ 1].flow -= f;
now += f;
res -= f;
if (!res) break;
}
}
}
return now;
}
int maxflow() {
int ans = 0;
while (BFS()) {
memset(cur,0,sizeof cur);
ans += DFS(s,0x3f3f3f3f);
}
return ans;
}
*/
int dep[N],gap[N];
void bfs() {
memset(dep,-1,sizeof dep);
memset(gap,0,sizeof gap);
dep[t] = 0;
gap[0] = 1;
queue <int> q;
q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto ed : G[u]) {
int y = ed.first,num = ed.second;
if (dep[y] != -1) continue;
q.push(y);
dep[y] = dep[u] + 1;
gap[dep[y]]++;
}
}
return ;
}
int mxflow;
int dfs(int u,int flow) {
if (u == t) {
mxflow += flow;
return flow;
}
int used = 0;
for (auto ed : G[u]) {
int d = ed.first,num = ed.second;
if (e[num].cap && dep[d] + 1 == dep[u]) {
int mi = dfs(d,min(e[num].cap,flow - used));
if (mi) {
e[num].cap -= mi;
e[num^1].cap += mi;
used += mi;
}
if (used == flow) return used;
}
}
--gap[dep[u]];
if(gap[dep[u]] == 0) dep[s] = n + m + 3;
dep[u]++;
gap[dep[u]]++;
return used;
}
int maxflow() {
mxflow = 0;
while (dep[s] < n+m+2) dfs(s,0x3f3f3f3f);
return mxflow;
}
void addedge(int x,int y,int cap) {
G[x].push_back(mp(y,++tot));
e[tot].cap = cap;
G[y].push_back(mp(x,++tot));
}
ll c,d;
int main() {
cin >> n >> m >> c >> d;
ll ans = 0,cnt = 0;
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= m;++j) {
char c;cin >> c;
if (c == '.') {
addedge(i,j+n,1);
++cnt;
}
}
}
s = n + m + 1,t = n + m + 2;
for (int i = 1;i <= n;++i) {
addedge(s,i,0);
}
for (int i = 1;i <= m;++i) {
addedge(n + i,t,0);
}
bfs();
ans = d * cnt;
ll res = 0;
for (int k = 1;k <= cnt;++k) {
for (auto ed : G[s]) {
int num = ed.second;
e[num].cap++;
}
for (auto ed : G[t]) {
int num = ed.second;
e[num^1].cap++;
}
ll now = maxflow();
res += now;
//cout << k << ":" << now << "\n";
ans = min(ans,c * k + d * (cnt - res));
}
cout << ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8436kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 5984kb
input:
3 4 1 2 .*** *..* **..
output:
5
result:
wrong answer 1st numbers differ - expected: '2', found: '5'