QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251841#5114. Cells ColoringHe2717970784AC ✓902ms9988kbC++174.8kb2023-11-15 10:32:052023-11-15 10:32:05

Judging History

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

  • [2023-11-15 10:32:05]
  • 评测
  • 测评结果:AC
  • 用时:902ms
  • 内存:9988kb
  • [2023-11-15 10:32:05]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#include<queue>
#define int long long
using namespace std;
const int N = 509, M = N * N,INF = INT_MAX;
char mp[N][N];
int head[N],pre[N],now[N],deep[N],gap[N],sc[N],cl[N];
int cnt = 1,s,t,pointnum;
struct Edge {
	int from, to, Next, w;
}edge[M << 1];

void addedge(int u, int v, int w) {
	cnt++;
	edge[cnt].from = u;
	edge[cnt].to = v;
	edge[cnt].w = w;
	edge[cnt].Next = head[u];
	head[u] = cnt;
}

void AddEdge(int u,int v,int w){
	addedge(u,v,w);
	addedge(v,u,0);
} 

void bfs(){//用于确定各顶点到汇点的距离
	//memset(gap, 0, sizeof(gap));//初始化间隙数组
	//memset(deep, 0, sizeof(deep));//所有点的高度初始为0
	deep[t] = 1;//汇点高度为1,其他点的高度均大于1
	queue<int>q;
	q.push(t);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		gap[deep[u]]++;//间隙数组 计算高度为deep[u]的节点个数
		for (int i = head[u]; i; i = edge[i].Next) {
			int v = edge[i].to;//v是u的邻居点
			if (edge[i ^ 1].w && (!deep[v])) {//反向边不用处理,高度不为0的点已经处理过了
				deep[v] = deep[u] + 1;
				q.push(v);
			}
		}
	}
}

int augment(){//沿着增广路径更新残留网络
	int v = t, flow = INF;
	while(v != s){//找到这条路径的流量
		int u = pre[v];//u是v向源点s方向回退的上一个点,相当于dfs的回溯
		if (edge[u].w < flow) {//路径上的最小容量就是这条路径的流量
			flow = edge[u].w;
		}
		v = edge[u].from;//更新v,继续回退
	}
	v = t;
	while (v != s) {//更新残留网络
		int u = pre[v];//向源点s方向回退
		edge[u].w -= flow;//正向边
		edge[u ^ 1].w += flow;//反向边
		v = edge[u].from;//更新v,继续回退
	}
	return flow;//返回这条路径的流量
}

int ISAP() {
	bfs();//求每个点到汇点的距离
	int flow = 0;//计算最大流
	int u = s;//从源点s开始寻找增广路径
	memcpy(now, head, sizeof(head));//当前弧优化,now是head的副本
	while (deep[s] <= pointnum) {//最大距离为n
		if (u == t) {//找到了一条增广路径
			flow += augment();//更新残留网络,更新流量
			u = s;//回到s,重新开始找一条增广路径
		}
		bool flag = false;//用于判断是否能顺利往下走
		for (int i = now[u]; i; i = edge[i].Next) {//在i的邻居中确定路径的下一个点
			int v = edge[i].to;//v是u的邻居点
			if(edge[i].w && deep[v] + 1 == deep[u]){//沿着高度递减方向找下一个点
				flag = true;//顺利找到了路径的下一个点
				pre[v] = i;//记录路径
				now[u] = i;//记录当前弧,下一次跳过它
				u = v;//u更新为路径的下一个点v
				break;//退出for循环,回到while循环,继续找路径的下一个点
			}
		}
		if (!flag) {//路径走不下去了,需要更新u的高度重新走
			gap[deep[u]]--;
			if (!gap[deep[u]]) {
				break;//u的下一个深度的点没有了,断层,退出while循环
			}
			int MinDeep = pointnum + 10;//用于计算u的邻居点的最小高度初值比n大就可以
			for (int i = head[u]; i; i = edge[i].Next) {//在u的邻居中寻找最小的高度
				int v = edge[i].to;
				if (edge[i].w && deep[v] < MinDeep) {
					MinDeep = deep[v];
				}
			}
			deep[u] = MinDeep + 1;//更新u的高度,改为v的高度加1,从而能够生成一条路径,继续往后走
			gap[deep[u]]++;//更新间隙数组,高度为deep[u]的节点个数加1
			now[u] = head[u];//记录当前弧
			if (u != s) {
				u = edge[pre[u]].from;//回退一步,相当于dfs的回溯
			}
		}
	}
	return flow;
}

void solve() {
	int c = 0, d = 0, K = 0, sum = 0,n = 0,m = 0;
	cin >> n >> m >> c >> d;
	for (int i = 1; i <= n; i++) {
		cin >> (mp[i] + 1);
		for (int j = 1; j <= m; j++) {
			if (mp[i][j] == '.') {
				mp[i][j] = 1;
			}
			else {
				mp[i][j] = 0;
			}
			sc[i] += mp[i][j];
			cl[j] += mp[i][j];
			K = max(K, cl[j]);
		}
		K = max(K, sc[i]);
		sum += sc[i];
	}
	s = n + m + 1;
	t = pointnum = s + 1;
	for (int i = 1; i <= n; i++) {
		AddEdge(s, i, 0);
	}
	for (int i = 1; i <= m; i++) {
		AddEdge(i + n, t, 0);
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (mp[i][j]) {
				AddEdge(i, j + n, 1);
			}
		}
	}
	int ans = LLONG_MAX,tmp = 0;
	for (int k = 0; k <= K; k++) {
		if (k) {
			memset(gap + 1, 0, sizeof(*gap) * pointnum);
			memset(deep + 1, 0, sizeof(*deep) * pointnum);
			for (int i = 1; i <= n; i++) {
				edge[i * 2].w++;
			}
			for (int i = 1; i <= m; i++) {
				edge[(i + n) * 2].w++;
			}
		}
		tmp += ISAP();
		ans = min(ans, c * k + d * (sum - tmp));
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 178ms
memory: 7448kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 259ms
memory: 6404kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 724ms
memory: 9988kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 765ms
memory: 9120kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: 0
Accepted
time: 179ms
memory: 7532kb

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 29ms
memory: 6032kb

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

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

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 40ms
memory: 4760kb

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 103ms
memory: 6944kb

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

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

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

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

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 276ms
memory: 7564kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 772ms
memory: 8284kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #17:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 902ms
memory: 9668kb

input:

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

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

score: 0
Accepted
time: 742ms
memory: 8556kb

input:

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

output:

250000000000

result:

ok 1 number(s): "250000000000"