QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#206887#5114. Cells ColoringKULIANLENTL 1ms10724kbC++202.0kb2023-10-07 23:48:452023-10-07 23:48:45

Judging History

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

  • [2023-10-07 23:48:45]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:10724kb
  • [2023-10-07 23:48:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int ni=2e5;
#define int long long
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,1e18))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;
}
signed 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++)
        f[i<<1]++;
        // printf("%d %d\n",i,cnt);
        ans=min(ans,c*i+d*cnt);
        if(k*d<=c)break;
    }
    printf("%lld\n",ans);
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 10724kb

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 10352kb

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: