QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#419050#5114. Cells ColoringLLCSAC ✓775ms9880kbC++173.2kb2024-05-23 17:17:442024-05-23 17:17:49

Judging History

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

  • [2024-05-23 17:17:49]
  • 评测
  • 测评结果:AC
  • 用时:775ms
  • 内存:9880kb
  • [2024-05-23 17:17:44]
  • 提交

answer

/*
@Date    : 2024-05-23 15:59:04
@Author  : Adscn ([email protected])
@Link    : http://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
template<typename T>IL bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
template<typename T>IL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>
IL T geti()
{
	RG T xi=0;
	RG char ch=gc;
	bool f=0;
	while(!isdigit(ch))ch=='-'?f=1:f,ch=gc;
	while(isdigit(ch))xi=xi*10+ch-48,ch=gc;
	return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
	if(k<0)k=-k,putchar('-');
	if(k>=10)pi(k/10);
	putchar(k%10+'0');
	if(ch)putchar(ch);
}
/*
IL unsigned int LOG2(unsigned int x)
{
	unsigned int ret;
	__asm__ __volatile__ ("bsrl %1, %%eax":"=a"(ret):"m"(x));
	return ret;
}
*/
const int N=251;
const int V=N*N*2+3*N;
const int E=N*N*4;
struct edge{
	int v,nxt,w;
}e[E<<1];
int head[V],cnt;
void init(){
	memset(head,cnt=-1,sizeof head);
}
void add(int u,int v,int w){
	e[++cnt]=(edge){v,head[u],w};
	head[u]=cnt;
}
void link(int u,int v,int w){
	add(u,v,w);
	add(v,u,0);
}
int dis[V];
int cur[V];
int S,T;
bool bfs(){
	queue<int>q;
	memset(dis,0,sizeof dis);
	q.push(S);
	memcpy(cur,head,sizeof head);
	dis[S]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=head[u];~i;i=e[i].nxt){
			int v=e[i].v;
			if(!dis[v]&&e[i].w!=0){
//				printf("(%d,%d,%d)\n",u,v,e[i].w);
				dis[v]=dis[u]+1;
				if(v==T)return 1;
				q.push(v);
			}
		}
	}
	return 0;
}
int dfs(int maxflow,int x){
//	printf("maxflow=%d x=%d\n",maxflow,x);
	if(!maxflow||x==T)return maxflow;
	int resflow=maxflow;
	for(int &i=cur[x];~i;i=e[i].nxt){
		int v=e[i].v,flow=0;
		assert(x!=v);
		if(e[i].w&&dis[v]==dis[x]+1&&(flow=dfs(min(resflow,e[i].w),v))){
			resflow-=flow;
			e[i].w-=flow;
			e[i^1].w+=flow;
			if(!resflow)break;
		}
	}
	return maxflow-resflow;
}
char c[N][N];
int n,m,maxk;
int id_row(int id){
	return id;
}
int id_col(int id){
	return id+n;
}
int id_point(int x,int y){
	return n+m+(x-1)*m+y;
}
int main(void)
{
	#ifndef ONLINE_JUDGE
//	File("");
	#endif
	init();
	n=gi,m=gi;
	ll C=gi,d=gi;
	int cnt=0;
	for(int i=1;i<=n;++i){
		scanf("%s",c[i]+1);
		for(int j=1;j<=m;++j)
			if(c[i][j]=='.')
				cnt++;
	}
	maxk=max(n,m);
	S=0,T=maxk*maxk*2+n+m+1;
	ll ans=d*cnt;
	ll flow=0;
	for(int i=1;i<=n;++i){
		link(S,id_row(i),1);
	}
	for(int i=1;i<=m;++i){
		link(id_col(i),T,1);
	}
	for(int i=1;i<=m;++i)
		for(int j=1;j<=n;++j)
			if(c[j][i]=='.')
				link(id_row(j),id_point(j,i),1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(c[i][j]=='.')
				link(id_point(i,j),id_col(j),1);
	for(int k=1;k<=maxk;++k){
		while(bfs()){
			int res=dfs(1e9,S);
//			printf("Res=%d\n",res);
//			printf("\n\n");
			flow+=res;
		}
//		printf("k=%d flow=%d\n",k,flow);
		chkmin(ans,k*C+d*(cnt-flow));
		for(int i=0;i<n+m;++i)e[i*2].w++;
	}
	pi(ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 6932kb

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 255ms
memory: 8584kb

input:

250 250 965680874 9042302
..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.***
....**.*******.*.******...

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 348ms
memory: 7872kb

input:

250 250 62722280 506434
*.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..*
.*..********..*...*..****...

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 705ms
memory: 8972kb

input:

250 250 85956327 344333
..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..**
.........*......*.*.........

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 743ms
memory: 9880kb

input:

250 250 768323813 489146
...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*......
...................*.......

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: 0
Accepted
time: 255ms
memory: 7636kb

input:

250 250 865365220 7248935
.....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*.
*..*.**.*...****.*.**.*...

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 61ms
memory: 7008kb

input:

153 225 485767021 308782855
.*.**.***..***..***..****.*****.***.....*.***.****.*.*......**......****.****.**.******...**...*.***.*..**.*****.****....*.*.*...**..****.**.******....*....****....**.*.*****.**.**.**.***...*.**.*.**.****.*.*....*.*****...***
*.*....*...*.*..*.*****.***.***.***.***..*.***...

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

score: 0
Accepted
time: 3ms
memory: 6868kb

input:

17 20 823772868 410753944
.....*......**......
.......*............
...............*....
......*.............
...................*
*........*.*..*.....
.....*.............*
..*..........*.*....
.......*............
...**...........**.*
....................
**......**.......*..
.*.............*....
....

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 83ms
memory: 6440kb

input:

227 129 426009970 614728889
*..****..*..*.********.*.*..***.*.*..**..*.***.**.**.***..*.***..*..*.***.*.*.**..*****.**..***.*.****.**.***.****..**.**.*.**.**
*...*****.**....****..*....*.*.**.*****.*..*****...*...**...******..****.*..**...***.*.*.*..*.*.*..*.******.*****..*.**********.*
.*.*..**..**...

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 121ms
memory: 7700kb

input:

170 219 28214303 818736602
*..*....*..*..*....**.*...*..*.**....**.*..*........*.**.....*....*.*..*..*.**.....*..***......*.....*...*........**.*.*.***...*........**..**....***...**....*.*..........**....*....**.***....*.*.*..*..***.....*.*..***.
.*.*.....**...*..*....*.*....*.*.**.........*...*..*....

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

score: 0
Accepted
time: 8ms
memory: 6964kb

input:

221 2 186883458 764869506
*.
.*
**
.*
**
**
*.
.*
.*
.*
*.
.*
..
**
**
*.
..
.*
.*
**
**
*.
.*
*.
..
*.
**
.*
**
..
**
..
**
..
.*
*.
**
.*
.*
**
..
.*
**
**
**
**
.*
..
.*
.*
..
**
.*
**
.*
**
..
**
*.
**
..
.*
*.
.*
**
.*
**
..
.*
**
.*
**
**
.*
**
**
.*
**
**
..
..
.*
**
..
**
.*
*.
*.
*.
*.
*.
*...

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

28 2 127093639 70848307
.*
.*
**
..
..
.*
**
*.
**
..
**
*.
*.
**
*.
..
.*
.*
*.
**
**
.*
**
..
*.
.*
**
**

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

score: 0
Accepted
time: 8ms
memory: 5760kb

input:

142 1 465099485 99077573
*
.
.
*
*
*
*
*
.
*
.
*
.
*
.
.
.
.
*
*
*
*
*
.
*
.
.
*
*
*
.
*
.
.
.
.
.
*
*
*
*
*
*
*
*
.
*
.
*
.
.
*
*
.
*
*
*
*
.
.
*
*
.
*
*
*
*
*
*
*
.
.
.
*
*
*
*
*
*
.
*
.
*
.
*
*
*
*
.
.
.
*
*
.
*
.
.
*
.
*
.
.
.
*
.
.
*
.
.
.
.
*
.
*
.
*
*
*
*
*
*
*
.
*
*
*
*
.
*
.
.
.
.
*
.
*
*
....

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 330ms
memory: 6956kb

input:

250 250 420456041 0
...*****.....****......*.**..*..*.**.**...*.***..**.*......*.*.....**..*..**.*..***.*.****.*.**.*.....**..*.*.**....**..***......*...***..*...****.*****.*.***.**.*.*...****.***..*...*.*.**.***..*.***.*.****.*.*...**....***....*.*.**....***...*.***...
*.**...**.**...*..*..*.*.*.**...

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 775ms
memory: 7984kb

input:

250 250 0 577876919
.............**.*.....*.....*.*..*.......*...*.................**........*........*....*...*.......*...*........................*.......*.....*.*.*.......*........................*...............*..*.*....*.*.*..................*.....................
...*...*...................*....

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 7ms
memory: 7216kb

input:

250 250 708109405 398540228
**********************************************************************************************************************************************************************************************************************************************************
*********************...

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 754ms
memory: 9048kb

input:

250 250 1000000000 1000000
..........................................................................................................................................................................................................................................................
.........................

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

score: 0
Accepted
time: 751ms
memory: 9600kb

input:

250 250 1000000000 10000000
..........................................................................................................................................................................................................................................................
........................

output:

250000000000

result:

ok 1 number(s): "250000000000"