QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#184951#5114. Cells Coloringzwp1234AC ✓287ms5868kbC++174.0kb2023-09-21 14:41:342023-09-21 14:41:35

Judging History

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

  • [2023-09-21 14:41:35]
  • 评测
  • 测评结果:AC
  • 用时:287ms
  • 内存:5868kb
  • [2023-09-21 14:41:34]
  • 提交

answer

//# pragma GCC optimize(2)
#include <bits/stdc++.h>
//#define int long long
#define SZ(x) (int)(x).size()
#define fs first
#define sc second
#define PII pair<int,int>
#define ls(u) (u)<<1
#define rs(u) (u)<<1|1
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vi >
#define vI vector<PII>
#define db double
#define all(a) (a).begin(),(a).end()
#define die cout << "???" << endl
//#define endl '\n'
using namespace std;
const int mod = 998244353;
//const int mod = 1169996969;
//const int mod = 1e9+7;
//const int inf = LLONG_MAX;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
//const int inf = 1e9;
//const db PI = acos(-1.0);
const db eps = 1e-5;
typedef unsigned long long  ull;
const int N = 3e2+5,M = 2e5+5,K = 605;

inline int rd() {
    int f = 0; int x = 0; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
    for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    if (f) x = -x;
    return x;
}

void write(int a) {if(a>=10)write(a/10);putchar(a%10+'0');}
void wt(int a) {if(a < 0) {putchar('-');a = -a;}write(a);}
void wwt(int a){wt(a);putchar('\n');}

// void write(ll a) {if(a>=10)write(a/10);putchar(a%10+'0');}
// void wt(ll a) {if(a < 0) {putchar('-');a = -a;}write(a);}
// void wwt(ll a){wt(a);putchar('\n');}

int lowbit(int x) {return (x&(-x));}
int n,m,k;
int S,T;

char s[N][N];
int col[N],row[N];

int h[K],e[M],ne[M],f[M],tot;

void add(int u,int v, int x)
{
    e[tot] = v,ne[tot] = h[u],f[tot] = x,h[u] = tot++;
    e[tot] = u,ne[tot] = h[v],f[tot] = 0,h[v] = tot++;
}

int dep[K],cur[K],gap[N];

bool bfs()
{
    for(int i = S;i<=T;i++)
    {
        dep[i] = -1;
        cur[i] = -1;
    }

    queue<int> q;

    dep[S] = 0;
    cur[S] = h[S];

    q.push(S);

    while(!q.empty())
    {
        int u = q.front();
        q.pop();

        for(int i = h[u];~i;i = ne[i])
        {
            int v = e[i];
            if(dep[v] == -1 && f[i])
            {
                dep[v] = dep[u] + 1;
                cur[v] = h[v];
                q.push(v);
            }
        }
    }

    return dep[T] != -1;
}

int dfs(int u,int a)
{
    if(u == T || a == 0) return a;

    int flow = 0,tmp;

    for(int& i = cur[u];~i;i = ne[i])
    {
        int v = e[i];
        if(dep[v] == dep[u] + 1 && (tmp = dfs(v,min(a,f[i]))))
        {
            flow += tmp;
            f[i] -= tmp;
            f[i^1] += tmp;
            a -= tmp;

            if(a == 0)
            {
                break;
            }
        }
    }

    return flow;
}

int res;

void dinic()
{
    while(bfs())
    {
        res += dfs(S,inf);
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r+", stdin);
    freopen("out.txt", "w+", stdout);
    #endif

    int c,d;
    
    cin >> n >> m >> c >> d;

    int mxk = 0;

    res = 0;
    int sum = 0;

    S = 0;
    T = m + n + 1;

    for(int i = S;i<=T;i++)
    {
        h[i] = -1;
    }

    for(int i = 1;i<=n;i++)
    {
        cin >> (s[i] + 1);
        for(int j = 1;j<=m;j++) s[i][j] = s[i][j] == '.',row[i] += s[i][j],col[j] += s[i][j],mxk = max(mxk,col[j]),sum += s[i][j];
        mxk = max(mxk,row[i]);
    }

    for(int i = 1;i<=n;i++) add(S,i,0);
    for(int i = 1;i<=m;i++) add(i + n ,T,0);

    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            if(s[i][j])
            {
                add(i,j+n,1);
            }
        }
    }


    ll ans = INF;  

    for(int k = 0;k<=mxk;k++)
    {
        if(k)
        {
            for(int i = 0;i<n;i++) ++f[i*2];
            for(int i = 0;i<m;i++) ++ f[(i+n)*2];
        }

        dinic();
        ans = min(ans,1ll*c*k + 1ll*d*(sum - res));
    }

    cout << ans;

    return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 88ms
memory: 4192kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 97ms
memory: 5848kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 253ms
memory: 4908kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 265ms
memory: 4888kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

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

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 16ms
memory: 3876kb

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

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

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

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

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 33ms
memory: 5700kb

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

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

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

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

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 274ms
memory: 4952kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #17:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 281ms
memory: 5128kb

input:

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

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

score: 0
Accepted
time: 287ms
memory: 5108kb

input:

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

output:

250000000000

result:

ok 1 number(s): "250000000000"