QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#76623#5114. Cells ColoringperspectiveAC ✓250ms15832kbC++233.2kb2023-02-11 04:20:512023-02-11 04:20:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-11 04:20:51]
  • 评测
  • 测评结果:AC
  • 用时:250ms
  • 内存:15832kb
  • [2023-02-11 04:20:51]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define REP1(i, n) for(ll i = 0; i < (n); i++)
#define REP2(i, a, b) for(ll i = (a); i < (b); i++)
#define REP0(n) for(ll iii = 0; iii < (n); iii++)
#define overload3(a, b, c, name, ...) name
#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)
#define si(c) (ll)(c.size())
#define pll pair<ll, ll>

#define vl vector<ll>
#define all(v) v.begin(), v.end()
#define fore(e, v) for(auto &&e : v)

template <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }
template <typename T, typename S> bool chmin(T &x, const S &y) { return (x > y ? x = y, true : false); }

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "{";
    rep(i, si(v)) {
        if(i) os << ", ";
        os << v[i];
    }
    return os << "}";
}

struct Dinic {
    const ll INF;

    struct edge {
        int to;
        ll cap;
        int rev;
        bool isrev;
        int idx;
    };

    vector<vector<edge>> g;
    vector<int> min_cost, iter;

    Dinic(int V) : INF(1e18), g(V) {}

    void add_edge(int from, int to, ll cap, int idx = -1) {
        g[from].emplace_back((edge){to, cap, (int)si(g[to]), false, idx});
        g[to].emplace_back((edge){from, 0, (int)si(g[from]) - 1, true, idx});
    }

    bool bfs(int s, int t) {
        min_cost.assign(g.size(), -1);
        queue<int> que;
        min_cost[s] = 0;
        que.push(s);
        while(!que.empty() and min_cost[t] == -1) {
            int p = que.front();
            que.pop();
            fore(e, g[p]) {
                if(e.cap > 0 and min_cost[e.to] == -1) {
                    min_cost[e.to] = min_cost[p] + 1;
                    que.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }

    ll dfs(int idx, const int t, ll flow) {
        if(idx == t) return flow;
        for(int &i = iter[idx]; i < g[idx].size(); i++) {
            edge &e = g[idx][i];
            if(e.cap > 0 and min_cost[idx] < min_cost[e.to]) {
                ll d = dfs(e.to, t, min(flow, e.cap));
                if(d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    ll max_flow(int s, int t) {
        ll flow = 0;
        while(bfs(s, t)) {
            iter.assign(g.size(), 0);
            ll f = 0;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
        return flow;
    }
};

int main() {
    ll n, m, c, d;
    cin >> n >> m >> c >> d;

    Dinic flow(n + m + 2);
    int s = n + m, t = s + 1;

    ll cnt = 0;
    rep(i, n) rep(j, m) {
        char c;
        cin >> c;
        if(c == '.') {
            cnt++;
            flow.add_edge(i, n + j, 1);
        }
    }

    ll mi = cnt * d;

    ll f = 0;
    rep(i, 1, max(n, m) + 1) {
        rep(i, n) flow.add_edge(s, i, 1);
        rep(i, m) flow.add_edge(n + i, t, 1);
        f += flow.max_flow(s, t);
        chmin(mi, i * c + (cnt - f) * d);
    }

    cout << mi << endl;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3372kb

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 119ms
memory: 15532kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 214ms
memory: 15832kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 211ms
memory: 15788kb

input:

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

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: 0
Accepted
time: 108ms
memory: 15244kb

input:

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

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

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

input:

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

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

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

input:

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

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 61ms
memory: 10888kb

input:

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

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 48ms
memory: 12880kb

input:

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

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

score: 0
Accepted
time: 21ms
memory: 7004kb

input:

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

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

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

input:

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

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

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

input:

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

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 211ms
memory: 15740kb

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #17:

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

input:

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

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 238ms
memory: 15288kb

input:

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

output:

62500000000

result:

ok 1 number(s): "62500000000"

Test #19:

score: 0
Accepted
time: 250ms
memory: 15288kb

input:

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

output:

250000000000

result:

ok 1 number(s): "250000000000"