QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#195007#5114. Cells ColoringShangguanyunxiaoWA 290ms6404kbC++203.0kb2023-10-01 00:05:352023-10-01 00:05:35

Judging History

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

  • [2023-10-01 00:05:35]
  • 评测
  • 测评结果:WA
  • 用时:290ms
  • 内存:6404kb
  • [2023-10-01 00:05:35]
  • 提交

answer

// Problem: B. Cells Coloring
// Contest: Codeforces - The 2022 ICPC Asia Xian Regional Contest
// URL: https://codeforces.com/gym/104077/problem/B
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using LL = long long;

constexpr int N = 600, M = 150000, INF = 1E8;

int n, m, S, T;
int h[N], e[M], f[M], ne[M], idx;
int q[N], d[N], cur[N];

void addEdge(int a, int b, int c) {
	e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void add(int a, int b, int c) {
    addEdge(a, b, c);
    addEdge(b, a, 0);
}

int find(int u, int limit) {
    if (u == T) {
    	return limit;
    }
    int flow = 0;
    for (int i = cur[u]; ~i and flow < limit; i = ne[i]) {
        cur[u] = i;
        int j = e[i];
        if (d[j] == d[u] + 1 and f[i]) {
            int t = find(j, std::min(f[i], limit - flow));
            if (t == 0) {
            	d[j] = -1;
            }
            f[i] -= t, f[i ^ 1] += t, flow += t;
        }
    }
    return flow;
}

bool bfs() {
    memset(d, -1, sizeof d);
    int hh = 0, tt = -1;
    q[ ++ tt] = S, d[S] = 0, cur[S] = h[S];
    while (hh <= tt) {
        auto t = q[hh ++ ];
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (d[j] == -1 && f[i]) {
                d[j] = d[t] + 1;
                cur[j] = h[j];
                if (j == T) {
                	return true;
                }
                q[ ++ tt] = j;
            }
        }
    }
    return false;
}

int dinic() {
    int res = 0, flow;
    while (bfs()) {
    	while (flow = find(S, INF)) {
    		res += flow;
    	}
    }
    return res;
}

constexpr LL inf = 1E18;

int g[N][N];

void solve() {
	int n, m, c, d;
	std::cin >> n >> m >> c >> d;
	
	for (int i = 1; i <= n; i += 1) {
		for (int j = 1; j <= m; j += 1) {
			char c;
			std::cin >> c;
			g[i][j] = c == '.' ? 0: 1;
		}
	}
	S = 0, T = n + m + 1;
	
	int up = 0, tot = 0;
	for (int i = 1; i <= n; i += 1) {
		int cnt = 0;
		for (int j = 1; j <= m; j += 1) {
			cnt += g[i][j] == 0;
		}
		tot += cnt;
		up = std::max(up, cnt);
	}
	for (int j = 1; j <= m; j += 1) {
		int cnt = 0;
		for (int i = 1; i <= n; i += 1) {
			cnt += g[i][j];
		}
		up = std::max(up, cnt);
	}
	
	LL ans = inf;
	for (int k = 0; k <= up; k += 1) {
		memset(h, -1, sizeof h), idx = 0;
		memset(f, 0, sizeof f);
		for (int i = 1; i <= n; i += 1) {
			add(S, i, k);
		}
		for (int j = 1; j <= m; j += 1) {
			add(n + j, T, k);
		}
		for (int i = 1; i <= n; i += 1) {
			for (int j = 1; j <= m; j += 1) {
				if (!g[i][j]) {
					add(i, n + j, 1);
				}
			}
		}
		int cnt = dinic();
		LL cost = 1LL * c * k + 1LL * d * (tot - cnt);
		ans = std::min(ans, cost);
	}
	
	std::cout << ans << "\n";
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	
    int T = 1;
    // std::cin >> T;
    while (T -- ) {
    	solve();
    }
    
    return 0;
}

详细

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 140ms
memory: 4992kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 147ms
memory: 5196kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 277ms
memory: 5464kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 290ms
memory: 5460kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

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

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 34ms
memory: 5804kb

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

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

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 49ms
memory: 6076kb

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 67ms
memory: 4776kb

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

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

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

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

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 142ms
memory: 5252kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: -100
Wrong Answer
time: 262ms
memory: 6132kb

input:

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

output:

2311507676

result:

wrong answer 1st numbers differ - expected: '0', found: '2311507676'