QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#782766#5114. Cells Coloringliuyz11AC ✓427ms14164kbC++142.4kb2024-11-25 21:21:272024-11-25 21:21:27

Judging History

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

  • [2024-11-25 21:21:27]
  • 评测
  • 测评结果:AC
  • 用时:427ms
  • 内存:14164kb
  • [2024-11-25 21:21:27]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define int long long 
using namespace std;

const int INF = 1e14;
const int MAXN = 1e4 + 5;
const int MAXM = 3e6 + 5;
queue<int> que;
int S, T, cnt, mincost;
int head[MAXN], cur[MAXN], nxt[MAXM], to[MAXM], value[MAXM], cost[MAXM];
int dep[MAXN],  s[MAXN], E[MAXN];
char st[MAXN];


void init(){
    cnt = 0;
    rep(i, S, T) head[i] = -1;
}

void add(int u, int v, int w){
    nxt[cnt] = head[u];
    head[u] = cnt;
    to[cnt] = v;
    value[cnt] = w;
    cnt++;
}

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

bool bfs(){
    while(!que.empty()) que.pop();
    rep(i, S, T) dep[i] = 0;
    que.push(S);
    dep[S] = 1;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int e = head[u]; e != -1; e = nxt[e]){
            int v = to[e];
            if(dep[v] || !value[e]) continue;
            dep[v] = dep[u] + 1;
            que.push(v);
        }
    }
    return dep[T];
}

int dfs(int u, int flow){
    if(u == T) return flow;
    for(int& e = cur[u]; e != -1; e = nxt[e]){
        int v = to[e];
        if(!value[e] || dep[v] != dep[u] + 1) continue;
        int q = dfs(v, min(flow, value[e]));
        if(q){
            value[e] -= q;
            value[e ^ 1] += q;
            return q;
        }
    }
    return 0;
}

int dinic(){
    int ans = 0;
    while(bfs()){
        rep(i, S, T) cur[i] = head[i];
        while(int d = dfs(S, INF)) ans += d;
    }
    return ans;
}

signed main(){
    int n, m, c, d;
    scanf("%lld%lld%lld%lld", &n, &m, &c, &d);
    S = 0, T = n + m + 1;
    init();
    int res = 0;
    rep(i, 1, n){
        scanf("%s", st);
        rep(j, 1, m){
            if(st[j - 1] == '.'){
                addedge(i, n + j, 1);
                res++;
                s[i]++;
                s[n + j]++;
            }
        }
    }
    int mx = 0;
    rep(i, 1, n){
        mx = max(mx, s[i]);
        E[i] = cnt;
        addedge(S, i, 0);
    }
    rep(i, 1, m){
        mx = max(mx, s[n + i]);
        E[n + i] = cnt;
        addedge(n + i, T, 0);
    }
    int ans = res * d;
    // printf("%d\n", ans);
    rep(i, 1, mx){
        rep(i, 1, n + m){
            value[E[i]]++;
        }
        res -= dinic();
        
        ans = min(ans, i * c + res * d);
    }
    printf("%lld\n", ans);
    return 0;
}

详细

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 87ms
memory: 12008kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

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

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 367ms
memory: 12336kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 377ms
memory: 12360kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: 0
Accepted
time: 86ms
memory: 12044kb

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

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

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

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

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 27ms
memory: 14016kb

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

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

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

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

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

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

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 122ms
memory: 12004kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 392ms
memory: 14100kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #17:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 427ms
memory: 14164kb

input:

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

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

score: 0
Accepted
time: 423ms
memory: 12092kb

input:

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

output:

250000000000

result:

ok 1 number(s): "250000000000"