QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188377#5489. Room Evacuationtriplem5ds#WA 7ms10844kbC++234.7kb2023-09-25 19:32:202023-09-25 19:32:21

Judging History

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

  • [2023-09-25 19:32:21]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:10844kb
  • [2023-09-25 19:32:20]
  • 提交

answer

/// Msaa el 5ra
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include "bits/stdc++.h"

using namespace std;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x,y)
#define popCnt(x) (__builtin_popcountll(x))
#define int ll

using ll = long long;
using ii = pair<int, int>;
using ull = unsigned long long;

const int N = 5e5 + 5, LG = 18, MOD = 1e9 + 7;
const long double PI = acos(-1);
const long double EPS = 1e-7;

struct edge {
    int from, to, cap, flow, index;

    edge(int from, int to, int cap, int flow, int index) :
            from(from), to(to), cap(cap), flow(flow), index(index) {}
};

struct PushRelabel {
    int n;
    vector<vector<edge> > g;
    vector<long long> excess;
    vector<int> height, active, count;
    queue<int> Q;

    PushRelabel(int n) :
            n(n), g(n), excess(n), height(n), active(n), count(2 * n) {}

    void addEdge(int from, int to, int cap) {
        g[from].push_back(edge(from, to, cap, 0, g[to].size()));
        if (from == to)
            g[from].back().index++;
        g[to].push_back(edge(to, from, 0, 0, g[from].size() - 1));
    }

    void enqueue(int v) {
        if (!active[v] && excess[v] > 0) {
            active[v] = true;
            Q.push(v);
        }
    }

    void push(edge &e) {
        int amt = (int) min(excess[e.from], (long long) e.cap - e.flow);
        if (height[e.from] <= height[e.to] || amt == 0)
            return;
        e.flow += amt;
        g[e.to][e.index].flow -= amt;
        excess[e.to] += amt;
        excess[e.from] -= amt;
        enqueue(e.to);
    }

    void relabel(int v) {
        count[height[v]]--;
        int d = 2 * n;
        for (auto &it: g[v]) {
            if (it.cap - it.flow > 0)
                d = min(d, height[it.to] + 1);
        }
        height[v] = d;
        count[height[v]]++;
        enqueue(v);
    }

    void gap(int k) {
        for (int v = 0; v < n; v++) {
            if (height[v] < k)
                continue;
            count[height[v]]--;
            height[v] = max(height[v], n + 1);
            count[height[v]]++;
            enqueue(v);
        }
    }

    void discharge(int v) {
        for (int i = 0; excess[v] > 0 && i < g[v].size(); i++)
            push(g[v][i]);
        if (excess[v] > 0) {
            if (count[height[v]] == 1)
                gap(height[v]);
            else
                relabel(v);
        }
    }

    long long max_flow(int source, int dest) {
        count[0] = n - 1;
        count[n] = 1;
        height[source] = n;
        active[source] = active[dest] = 1;
        for (auto &it: g[source]) {
            excess[source] += it.cap;
            push(it);
        }

        while (!Q.empty()) {
            int v = Q.front();
            Q.pop();
            active[v] = false;
            discharge(v);
        }

        long long max_flow = 0;
        for (auto &e: g[source])
            max_flow += e.flow;

        return max_flow;
    }
};

int n, m, t;
char grid[22][22];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int getId(int i, int j, int t, bool o) {
    int val = n * m * t;
    val += i * m + j;
    return val * 2 + o;
}

void doWork() {
    cin >> n >> m >> t;
    f(i, 0, n)f(j, 0, m) cin >> grid[i][j];
    PushRelabel flw(n * m * (t + 1) * 2 + 2);
    int src = n * m * (t + 1) * 2;
    int snk = n * m * (t + 1) * 2 + 1;
    f(i, 0, n)
        f(j, 0, m) if (grid[i][j] != '#') {
                if (grid[i][j] == 'P')flw.addEdge(src, getId(i, j, 0, 0), 1);
                if (grid[i][j] == 'E') {
                    f(x, 0, t + 1)flw.addEdge(getId(i, j, x, 1), snk, 1);
                }
                f(x, 0, t + 1)flw.addEdge(getId(i, j, x, 0), getId(i, j, x, 1), 1);
                f(k, 0, 4) {
                    int ni = i + dx[k];
                    int nj = j + dy[k];
                    if (ni >= 0 && nj >= 0 && ni < n && nj < m && grid[ni][nj] != '#') {
                        f(x, 0, t) {
                            flw.addEdge(getId(i, j, x, 1), getId(ni, nj, x + 1, 0), 1);
                        }
                    }
                }
            }

    cout << flw.max_flow(src, snk) << '\n';

}

int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE

    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 5 3
.....
..P#.
..PPE
..P.E

output:

4

result:

ok single line: '4'

Test #2:

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

input:

3 3 5
...
P#P
P#E

output:

2

result:

ok single line: '2'

Test #3:

score: -100
Wrong Answer
time: 7ms
memory: 10844kb

input:

20 15 53
PPEP..PP#P.#P.#
P.###EP#PEPP.E#
.PEEPEEP#E#PPE#
PE#.##P#..PP.##
E.PPP.#E##EPE##
E#P#.EEE.EEPPPP
P.#PPPE.PPPP#.P
.##EEEE.#E.E.##
P..P#PEPPEE#.PP
EPEEEEE#E.E.E#P
##.E.EPP.PEE.EP
..E#.PE.#EP.#PE
EEEEPP.E.#.EEP.
..E#PPE.#EPEP..
EPP.#.PP#P#P#PE
EE#.PP.#EP.PPE#
EEPP####P#.#E.#
PP.EEE..###PEP.
#EP...

output:

88

result:

wrong answer 1st lines differ - expected: '89', found: '88'