QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344898#3810. LandscapingKhNURE_KIVI#AC ✓6ms6940kbC++143.7kb2024-03-05 18:04:482024-03-05 18:04:48

Judging History

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

  • [2024-03-05 18:04:48]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:6940kb
  • [2024-03-05 18:04:48]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = -1, inf = 1000111222;

namespace max_flow {
    const int max_n = 1e5;
    struct ed {
        int to, f;
    };

    vector <ed> e;
    vector <int> edge[max_n];

    inline void add_edge (int u, int v, int f) {
        edge[u].pb(len(e));
        e.pb(ed{v, f});
        edge[v].pb(len(e));
        e.pb(ed{u, 0});
    }

    inline void add_bidirectional_edge (int u, int v, int f) {
        add_edge(u,v,f);
        add_edge(v,u,f);
    }

    int d[max_n], p[max_n];
    queue <int> q;

    inline bool bfs (int n, int s, int t) {
        for (int i = 0; i < n; i++) {
            d[i] = inf;
            p[i] = 0;
        }
        q.push(s);
        d[s] = 0;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : edge[v]) {
                if (e[id].f > 0 && d[e[id].to] == inf) {
                    d[e[id].to] = d[v] + 1;
                    q.push(e[id].to);
                }
            }
        }
        return d[t] != inf;
    }

    inline int dfs (int v, int s, int flow = inf) {
        if (!flow) {
            return 0;
        }
        if (v == s) {
            return flow;
        }
        for (; p[v] < len(edge[v]); ) {
            int id = edge[v][p[v]];
            int to = e[id].to;
            if (d[to] == d[v] + 1 && e[id].f > 0) {
                int res = dfs(to, s, min(flow, e[id].f));
                if (res) {
                    e[id].f -= res;
                    e[id ^ 1].f += res;
                    return res;
                }
            }
            ++p[v];
        }
        return 0;
    }


    inline ll solve (int n, int s, int t) {
        ll ans = 0;
        while (bfs(n, s, t)) {
            int f = dfs(s, t);
            while (f) {
                ans += f;
                f = dfs(s, t);
            }
        }
        return ans;
    }
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n,m,a,b;
    cin>>n>>m>>a>>b;
    vector<string> s(n);
    for (int i=0;i<n;i++){
        cin>>s[i];
    }
    const int S=n*m,T=n*m+1,N=n*m+2;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            if (s[i][j]=='#'){
                max_flow::add_bidirectional_edge(i*m+j,T,b);
            }
            else{
                max_flow::add_bidirectional_edge(i*m+j,S,b);
            }

            if (i+1<n){
                max_flow::add_bidirectional_edge(i*m+j,(i+1)*m+j,a);
            }
            if (j+1<m){
                max_flow::add_bidirectional_edge(i*m+j,i*m+(j+1),a);
            }
        }
    }
    cout<<max_flow::solve(N,S,T)<<"\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6300kb

input:

5 4 1000 2000
...#
#..#
...#
##..
###.

output:

11000

result:

ok single line: '11000'

Test #2:

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

input:

50 50 1 1
####################.###########################.#
#.####..#.###########.############################
########################################.#.#######
##.##..#####.######.#..############.########.#####
#########.########.################.###########.##
###############.###.###########.###...

output:

237

result:

ok single line: '237'

Test #3:

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

input:

50 50 1 2
....#.........##............#........#.........#.#
...........##..........##...............#....#...#
..##...............#....##...............#.......#
..............#...#....#....#..........#.......#..
#.##...................#......#...................
..........#...................#.......

output:

476

result:

ok single line: '476'

Test #4:

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

input:

50 50 1 1
....#.........##............#........#.........#.#
...........##..........##...............#....#...#
..##...............#....##...............#.......#
..............#...#....#....#..........#.......#..
#.##...................#......#...................
..........#...................#.......

output:

239

result:

ok single line: '239'

Test #5:

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

input:

50 50 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###...

output:

121500000

result:

ok single line: '121500000'

Test #6:

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

input:

50 50 100000 100000
##################################################
#.################################################
#.################################################
#.################################################
#.################################################
#.#######################...

output:

9800000

result:

ok single line: '9800000'

Test #7:

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

input:

50 50 100000 100000
##################################################
##################################################
##################################################
##################################################
##################################################
#########################...

output:

0

result:

ok single line: '0'

Test #8:

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

input:

1 1 1 1
.


output:

0

result:

ok single line: '0'

Test #9:

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

input:

49 50 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###...

output:

119100000

result:

ok single line: '119100000'

Test #10:

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

input:

50 49 100000 100000
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.
##..##......###.#.####..###.##.#..##..#.#####.#.#
##..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..#
#####..#....###..#.#.#.#..##...##.##.#.##.#####.#
#########..##.#.#.#....#.###.##..##..#...##.#...#
.#..###..###.#.###.#..##...###...

output:

119000000

result:

ok single line: '119000000'

Test #11:

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

input:

50 1 2 1
#
.
#
#
#
.
.
#
.
#
.
#
.
#
#
#
#
#
.
#
.
.
.
#
.
.
.
.
#
.
#
#
#
.
#
#
.
#
.
#
#
#
.
#
.
.
#
#
.
#

output:

20

result:

ok single line: '20'

Test #12:

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

input:

4 4 1000 1500
....
.#..
.#..
####


output:

7000

result:

ok single line: '7000'

Test #13:

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

input:

1 50 2 1
...#.#.##.#....#.####.##.#.########..#...##..#..##

output:

19

result:

ok single line: '19'

Test #14:

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

input:

5 5 1 10
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.


output:

40

result:

ok single line: '40'

Test #15:

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

input:

5 5 10 1
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.


output:

12

result:

ok single line: '12'

Test #16:

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

input:

50 50 1 10
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...

output:

4900

result:

ok single line: '4900'

Test #17:

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

input:

50 50 10 1
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...

output:

1250

result:

ok single line: '1250'

Test #18:

score: 0
Accepted
time: 4ms
memory: 6272kb

input:

50 50 1 2
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###.#...#.#.....

output:

2099

result:

ok single line: '2099'

Test #19:

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

input:

50 50 1 1
#.###..#.#.#.#####.#...#....#.###.##.#.###.#..##.#
#..##......###.#.####..###.##.#..##..#.#####.#.###
..##.##.#.#.....#..#...####.#...#..#.#.#.#.#..####
##..#....###..#.#.#.#..##...##.##.#.##.#####.#####
#####..##.#.#.#....#.###.##..##..#...##.#...#.#..#
##..###.#.###.#..##...###.#...#.#.....

output:

1215

result:

ok single line: '1215'

Test #20:

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

input:

50 50 1 2
####################.###########################.#
#.####..#.###########.############################
########################################.#.#######
##.##..#####.######.#..############.########.#####
#########.########.################.###########.##
###############.###.###########.###...

output:

472

result:

ok single line: '472'