QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#206391 | #5114. Cells Coloring | KULIANLEN | TL | 1ms | 6740kb | C++20 | 2.1kb | 2023-10-07 20:16:46 | 2023-10-07 20:16:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int ni=2e5;
int h[ni],e[ni],ne[ni],fr[ni],f[ni],idx;
void add(int a,int b,int c){
e[idx]=b;f[idx]=c;
ne[idx]=h[a];fr[idx]=a;
h[a]=idx++;
}
int bfs(),dfs(int,int);
long long n,m,c,d,ti,si,cnt;
long long ans=1e18;
int di[600];
int dinic(){
int maxflow=0,flow;
while(bfs()){
while(flow=dfs(si,30000))maxflow+=flow;
}
return maxflow;
}
queue<int>q;
int bfs(){
memset(di,0,sizeof(di));
while(q.size())q.pop();
q.push(si);di[si]=1;
while(q.size()){
int a=q.front();q.pop();
for(int i=h[a];i!=-1;i=ne[i]){
if(f[i]&&!di[e[i]]){
q.push(e[i]);
di[e[i]]=di[a]+1;
if(e[i]==ti)return 1;
}
}
}
return 0;
}
int dfs(int a,int flow){
if(a==ti)return flow;
int rest=flow,k;
for(int i=h[a];i!=-1;i=ne[i]){
if(f[i]&&di[e[i]]==di[a]+1){
k=dfs(e[i],min(rest,f[i]));
if(!k)di[e[i]]=0;
f[i]-=k;
f[i^1]+=k;
rest-=k;
}
}
return flow-rest;
}
int main(){
memset(h,-1,sizeof(h));
scanf("%lld%lld%lld%lld",&n,&m,&c,&d);
si=0;ti=n+m+1;
for(int i=1;i<=n;i++)
add(0,i,1),add(i,0,0);
for(int i=1;i<=m;i++)
add(n+i,ti,1),add(ti,n+i,0);
for(int i=1;i<=n;i++){
string s;cin>>s;
for(int j=0;j<m;j++)
if(s[j]=='.'){
add(i,j+1+n,1);
add(j+1+n,i,0);cnt++;
}
}
ans=cnt*d;
// printf("(%d)",cnt);
for(int i=1;i<=max(n,m);i++){
// printf("_____________________\n");
// for(int i=0;i<idx;i++){
// printf("%d %d %d %d\n",i,fr[i],e[i],f[i]);
// }
for(int i=0;i<n+m;i++)
f[i<<1]=1,f[i<<1|1]=0;
for(int i=n+m;i<idx;i++)
f[i<<1|1]=0;
int k=dinic();
cnt-=k;
// printf("%d %d\n",i,cnt);
ans=min(ans,c*i+d*cnt);
if(k*d<=c)break;
}
printf("%lld\n",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6656kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 6740kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Time Limit Exceeded
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...