QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206470#5114. Cells ColoringKULIANLENWA 0ms16504kbC++202.5kb2023-10-07 20:42:092023-10-07 20:42:09

Judging History

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

  • [2023-10-07 20:42:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:16504kb
  • [2023-10-07 20:42:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int ni=2e5;
int h[ni],e[ni],ne[ni],fr[ni],f[ni],idx,w[ni];
int cur[ni];
int dep[ni];
const long long inf=1e18;
int dis[ni];
int now[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[ni];        
inline int bfs() {  //在惨量网络中构造分层图 
	for(register int i=1;i<=n+m+2;i++) dis[i]=inf;
	queue<int> q;
	q.push(si);
	dis[si]=0;
	now[si]=h[si];
	while(!q.empty()) {
		int x=q.front();
		q.pop();
		for(register int i=h[x];~i;i=ne[i]) {
			int v=e[i];
			if(w[i]>0&&dis[v]==inf) {
				q.push(v);
				now[v]=h[v];
				dis[v]=dis[x]+1;
				if(v==ti) return 1;
			}
		}
	}
	return 0;
}

inline int dfs(int x,long long sum) {  //sum是整条增广路对最大流的贡献
	if(x==ti) return sum;
	long long k,res=0;  //k是当前最小的剩余容量 
	for(register int i=now[x];~i&&sum;i=ne[i]) {
		now[x]=i;  //当前弧优化 
		int v=e[i];
		if(w[i]>0&&(dis[v]==dis[x]+1)) {
			k=dfs(v,min(sum,w[i]));
			if(k==0) dis[v]=inf;  //剪枝,去掉增广完毕的点 
			w[i]-=k;
			w[i^1]+=k;
			res+=k;  //res表示经过该点的所有流量和(相当于流出的总量) 
			sum-=k;  //sum表示经过该点的剩余流量 
		}
	}
	return res;
}


int dinic()
{
    int ans = 0;
    memset(now,0,sizeof now);
    while(bfs())
    {
        ans+=dfs(si,inf);
    }    
    return ans;
}
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]);
        // }
        for(int i=0;i<n+m;i++)
        w[i<<1]=1,w[i<<1|1]=0;
        for(int i = n*2+m*2-1;i<idx;i++)
        if(i&1)w[i] = 0;
        else w[i] = 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: 0
Wrong Answer
time: 0ms
memory: 16504kb

input:

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

output:

5

result:

wrong answer 1st numbers differ - expected: '4', found: '5'