QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#206840#5114. Cells ColoringKULIANLENAC ✓354ms23948kbC++202.5kb2023-10-07 23:15:042023-10-07 23:15:04

Judging History

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

  • [2023-10-07 23:15:04]
  • 评测
  • 测评结果:AC
  • 用时:354ms
  • 内存:23948kb
  • [2023-10-07 23:15:04]
  • 提交

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;
}
string s[ni];

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++){
        cin >> s[i];
        for(int j = 0;j < m;j++)
        if(s[i][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++)
        w[i<<1]++,w[i<<1|1] = 0;
        
        // printf("%d %d\n",i,cnt);
        ans=min(ans,c * i + d * cnt);
    }
    printf("%lld\n",ans);
}

详细

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 84ms
memory: 23360kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 116ms
memory: 23836kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 292ms
memory: 22028kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 303ms
memory: 22924kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

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

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 26ms
memory: 23292kb

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

score: 0
Accepted
time: 5ms
memory: 23180kb

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 36ms
memory: 23536kb

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 42ms
memory: 22996kb

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

score: 0
Accepted
time: 14ms
memory: 21432kb

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

score: 0
Accepted
time: 6ms
memory: 22808kb

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 114ms
memory: 23304kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 324ms
memory: 23596kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 12ms
memory: 21520kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 354ms
memory: 23656kb

input:

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

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

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

input:

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

output:

250000000000

result:

ok 1 number(s): "250000000000"