QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206892#5114. Cells ColoringKULIANLENTL 2ms8216kbC++202.1kb2023-10-07 23:51:092023-10-07 23:51:10

Judging History

你现在查看的是最新测评结果

  • [2023-10-07 23:51:10]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:8216kb
  • [2023-10-07 23:51:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int ni=2e5;
int h[ni],e[ni],ne[ni],fr[ni],f[ni],idx,w[ni];
int cur[ni];
int dep[ni];

void add(int a,int b,int c){
    e[idx]=b;w[idx]=c;
    ne[idx]=h[a];fr[idx]=a;
    h[a]=idx++;
}

long long n,m,c,d,ti,si,cnt;
long long ans=1e18;
int di[600];        
bool bfs()
{
    memset(dep,-1,sizeof dep);
    dep[si] = 0;
    memcpy(cur,h,sizeof(h));
    queue<int> q;q.push(si);
    while(!q.empty())
    {
        auto t = q.front();q.pop();
        for(int i = h[t];~i;i=ne[i])
        {
            int j = e[i];
            
            if(w[i]>0 && dep[j]==-1)
            {
                dep[j] = dep[t]+1;q.push(j);
            }
        }
    }
    return dep[ti]!=-1;
}

int dfs(int p = si,int flow = 0x3f3f3f3f)
{
    if(p==ti)return flow;
    int res = flow;
    for(int i = cur[p];~i;i=ne[i])
    {
        cur[p] = i;
        int j = e[i];
        if(w[i]>0 && dep[j]==dep[p]+1)
        {
            int c = dfs(j,min(res,w[i]));
            res -= c;
            w[i] -= c;
            w[i^1]+=c;
        }
    }
    return flow-res;
}


int dinic()
{
    int ans = 0;
    while(bfs())
    {
        ans+=dfs();
    
    }    
    return ans;
}
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]);
        // }
        
        int k=dinic();
        cnt-=k;
        for(int i=0;i<n+m;i++)
        w[i<<1]++;
        // 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: 7780kb

input:

3 4 2 1
.***
*..*
**..

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 2ms
memory: 8216kb

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
..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.***
....**.*******.*.******...

output:


result: