QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#528052#6113. Window Arrangementfishy15WA 38ms4076kbC++174.8kb2024-08-23 05:48:582024-08-23 05:48:59

Judging History

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

  • [2024-08-23 05:48:59]
  • 评测
  • 测评结果:WA
  • 用时:38ms
  • 内存:4076kb
  • [2024-08-23 05:48:58]
  • 提交

answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <numeric>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INFLL 0x3f3f3f3f3f3f3f3f

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

#include <bits/extc++.h>

using namespace std;

using vi = vector<int>;

const ll INF = numeric_limits<ll>::max() / 4;

struct MCMF {
    struct edge {
        int from, to, rev;
        ll cap, cost, flow;
    };
    int N;
    vector<vector<edge>> ed;
    vi seen;
    vector<ll> dist, pi;
    vector<edge*> par;

    MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}

    void addEdge(int from, int to, ll cap, ll cost) {
        if (from == to) return;
        ed[from].push_back(edge {from,to,sz(ed[to]),cap,cost,0 });
        ed[to].push_back(edge {to,from,sz(ed[from])-1,0,-cost,0 });
    }

    void path(int s) {
        fill(all(seen), 0);
        fill(all(dist), INF);
        dist[s] = 0; ll di;

        __gnu_pbds::priority_queue<pair<ll, int>> q;
        vector<decltype(q)::point_iterator> its(N);
        q.push({ 0, s });

        while (!q.empty()) {
            s = q.top().second; q.pop();
            seen[s] = 1; di = dist[s] + pi[s];
            for (edge& e : ed[s]) if (!seen[e.to]) {
                ll val = di - pi[e.to] + e.cost;
                if (e.cap - e.flow > 0 && val < dist[e.to]) {
                    dist[e.to] = val;
                    par[e.to] = &e;
                    if (its[e.to] == q.end())
                        its[e.to] = q.push({ -dist[e.to], e.to });
                    else
                        q.modify(its[e.to], { -dist[e.to], e.to });
                }
            }
        }
        rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
    }

    pair<ll, ll> maxflow(int s, int t) {
        ll totflow = 0, totcost = 0;
        while (path(s), seen[t]) {
            ll fl = INF;
            for (edge* x = par[t]; x; x = par[x->from])
                fl = min(fl, x->cap - x->flow);

            totflow += fl;
            for (edge* x = par[t]; x; x = par[x->from]) {
                x->flow += fl;
                ed[x->to][x->rev].flow -= fl;
            }
        }
        rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
        return {totflow, totcost/2};
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, m;
    cin >> n >> m;

    vector p(n, vector<int>(m));
    vector w(n, vector<int>(m));
    rep(i, 0, n) {
        rep(j, 0, m) {
            cin >> p[i][j];
        }
    }
    rep(i, 0, n) {
        rep(j, 0, m) {
            cin >> w[i][j];
        }
    }

    auto room = [&](int x, int y) {
        return x * m + y;
    };

    // n x (m + 1) of these
    auto vert_wall = [&](int x, int y) {
        return n * m + x * (m + 1) + y;
    };

    // (n + 1) x m of these
    auto horz_wall = [&](int x, int y) {
        return n * m + n * (m + 1) + x * m + y;
    };

    auto left = [&](int x, int y) { return horz_wall(x, y); };
    auto right = [&](int x, int y) { return horz_wall(x, y+1); };
    auto up = [&](int x, int y) { return vert_wall(x, y); };
    auto down = [&](int x, int y) { return vert_wall(x+1, y); };

    int src = (n * m) + (n * (m + 1)) + ((n + 1) * m);
    int sink = src + 1;

    MCMF mcmf(sink + 1);
    rep(i, 0, n) {
        rep(j, 0, m) {
            mcmf.addEdge(src, room(i, j), w[i][j], 0);
            mcmf.addEdge(room(i, j), left(i, j), 1, 0);
            mcmf.addEdge(room(i, j), right(i, j), 1, 0);
            mcmf.addEdge(room(i, j), up(i, j), 1, 0);
            mcmf.addEdge(room(i, j), down(i, j), 1, 0);
        }
    }

    rep(i, 0, n) {
        rep(j, 0, m+1) {
            int betw_cost;
            if (j > 0 && j < m) {
                betw_cost = p[i][j - 1] * p[i][j];
            } else {
                betw_cost = 0;
            }

            mcmf.addEdge(vert_wall(i, j), sink, 1, 0);
            mcmf.addEdge(vert_wall(i, j), sink, 1, betw_cost);
        }
    }

    rep(i, 0, n+1) {
        rep(j, 0, m) {
            int betw_cost;
            if (i > 0 && i < n) {
                betw_cost = p[i - 1][j] * p[i][j];
            } else {
                betw_cost = 0;
            }

            mcmf.addEdge(horz_wall(i, j), sink, 1, 0);
            mcmf.addEdge(horz_wall(i, j), sink, 1, betw_cost);
        }
    }

    cout << mcmf.maxflow(src, sink).second << '\n';

    return 0;
}

詳細信息

Test #1:

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

input:

4 3
1 7 10
7 2 8
7 9 10
4 6 4
3 3 3
3 2 4
4 3 4
2 2 3

output:

178

result:

ok 1 number(s): "178"

Test #2:

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

input:

4 3
2 2 9
9 8 4
8 4 5
7 5 2
0 1 0
1 0 1
0 0 1
0 1 0

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 38ms
memory: 4076kb

input:

20 20
755 344 600 54 516 407 657 429 565 185 90 323 449 464 872 138 404 500 196 111
666 191 824 98 505 538 949 801 266 861 984 957 396 851 496 147 225 451 874 380
536 200 581 397 305 514 351 416 228 763 566 442 618 131 527 651 954 757 226 129
286 608 819 477 891 22 19 747 565 704 198 703 736 8 835 1...

output:

12792427

result:

wrong answer 1st numbers differ - expected: '14347279', found: '12792427'