QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#152303#5472. Secure the Top Secretideograph_advantageWA 4ms13836kbC++175.1kb2023-08-27 23:36:072023-08-27 23:36:08

Judging History

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

  • [2023-08-27 23:36:08]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:13836kb
  • [2023-08-27 23:36:07]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define iter(v) v.begin(), v.end()
#define SZ(v) (int)v.size()
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef LOCAL
void debug(){cerr << "\n";}
template<class T, class ... U>
void debug(T a, U ... b){ cerr << a << " ", debug(b...);}
template<class T> void pary(T l, T r){
    while(l != r) cerr << *l << " ", l++;
    cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif

template<class A, class B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.ff << ',' << p.ss << ')';
}

const ll MAX = 1e9;
#define maxn 250000
#define inf 1000000000
struct MCMF{
    struct edge {
        ll from, to, cap, flow, cost, rev;
    } * past[maxn];
    vector<edge> G[maxn];
    bitset<maxn>inq;
    ll dis[maxn], up[maxn], s, t, mx, n;
    bool BellmanFord(ll &flow, ll &cost) {
        fill(dis, dis+n, inf);
        queue<ll> q;
        q.push(s);
        inq.reset(), inq[s] = 1;
        up[s] = mx - flow, past[s] = 0, dis[s] = 0;
        while (!q.empty()) {
            ll u = q.front();
            q.pop(), inq[u] = 0;
            if (!up[u]) continue;
            for (auto &e :G[u]) {
                if (e.flow != e.cap && dis[e.to] > dis[u] + e.cost) {
                    dis[e.to] = dis[u] + e.cost, past[e.to] = &e;
                    up[e.to] = min(up[u], e.cap - e.flow);
                    if (!inq[e.to]) inq[e.to] = 1, q.push(e.to);
                }
            }
        }
        if (dis[t] == inf) return 0;
        flow += up[t], cost += up[t] * dis[t];
        for (ll i = t;past[i];i = past[i]->from) {
            auto &e = *past[i];
            e.flow += up[t], G[e.to][e.rev].flow -= up[t];
        }
        return 1;
    }
    ll MinCostMaxFlow(ll _s, ll _t, ll &cost) {
        s = _s, t = _t, cost = 0;
        ll flow = 0;
        while (BellmanFord(flow, cost));
        debug("ok", flow, cost);
        return flow;
    }
    void init(ll _n, ll _mx) {
        n = _n, mx = _mx;
        for (int i = 0;i < n;i++) G[i].clear();
    }
    void add_edge(ll a, ll b, ll cap, ll cost) {
        debug("add_edge", a, b, cap, cost);
        G[a].pb(edge{a, b, cap, 0, cost, G[b].size()});
        G[b].pb(edge{b, a, 0, 0, -cost, G[a].size() - 1});
    }
} flow;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cerr.tie(0);
    
    int n, m;
    cin >> n >> m;
    flow.init((n + 1) * (m + 1) + 2, 1e9);

    auto addedge = [&](int x1, int y1, int x2, int y2, int w){
        if(x1 == x2 && (x1 == 0 || x1 == n)) return;
        if(y1 == y2 && (y1 == 0 || y1 == m)) return;
        flow.add_edge(x1 * (m + 1) + y1, x2 * (m + 1) + y2, 2 - w, w);
        flow.add_edge(x2 * (m + 1) + y2, x1 * (m + 1) + y1, 2 - w, w);
    };
    pii S, U, T;
    for(int i = 1; i <= n; i++){
        string s;
        cin >> s;
        s = " " + s;
        for(int j = 1; j <= m; j++){
            if(s[j] == 'S') S = mp(i, j);
            if(s[j] == 'T') T = mp(i, j);
            if(s[j] == 'U') U = mp(i, j);
            if(s[j] != '#') continue;
            addedge(i, j, i, j - 1, 0);
            addedge(i, j, i - 1, j, 0);
            addedge(i - 1, j - 1, i, j - 1, 0);
            addedge(i - 1, j - 1, i - 1, j, 0);
        }
    }

    int K;
    cin >> K;
    for(int i = 0; i < K; i++){
        int x, y;
        char d;
        cin >> x >> y >> d;
        if(d == 'b') addedge(x, y - 1, x, y, 1);
        else addedge(x - 1, y, x, y, 1);
    }

    vector<pii> dir = {mp(0, 1), mp(1, 0), mp(-1, 0), mp(0, -1)};

    vector<vector<int>> bor(n + 2, vector<int>(m + 2));
    function<void(int, int, int, int, int)> dfs_bor = [&](int x, int y, int t, int px, int py){
        debug("bor", x, y, t);
        if(x == 1) bor[x - 1][y - 1] |= t, bor[x - 1][y] |= t;
        if(x == n) bor[x][y - 1] |= t, bor[x][y] |= t;
        if(y == 1) bor[x - 1][y - 1] |= t, bor[x][y - 1] |= t;
        if(y == m) bor[x - 1][y] |= t, bor[x][y] |= t;
        for(auto [dx, dy] : dir){
            int nx = x + dx, ny = y + dy;
            if(nx == px && ny == py) continue;
            if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
            if(x == nx && x != 1 && x != n) continue;
            if(y == ny && y != 1 && y != m) continue;
            if(mp(nx, ny) == S || mp(nx, ny) == T || mp(nx, ny) == U) continue;
            dfs_bor(nx, ny, t, x, y);
        }
    };
    //STU
    dfs_bor(S.ff, S.ss, 0b100, S.ff, S.ss);
    dfs_bor(T.ff, T.ss, 0b010, T.ff, T.ss);
    dfs_bor(U.ff, U.ss, 0b001, U.ff, U.ss);

    int _s = (n + 1) * (m + 1), _t = _s + 1;
    ll ans = MAX;
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= m; j++){
            if(bor[i][j] == 0b101)
                flow.add_edge(_s, i * (m + 1) + j, 2, 0);
            if(bor[i][j] == 0b011)
                flow.add_edge(i * (m + 1) + j, _t, 2, 0);
        }
    }
    if(flow.MinCostMaxFlow(_s, _t, ans) < 2) cout << "-1\n";
    else cout << ans << "\n";
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 11788kb

input:

3 3
S..
#..
U.T
7
1 2 b
1 3 b
2 2 b
2 2 r
2 3 b
3 1 r
3 2 r

output:

3

result:

ok single line: '3'

Test #2:

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

input:

2 2
ST
.U
4
1 1 r
1 1 b
1 2 b
2 1 r

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

7 10
U.........
..........
###.......
..........
.......###
..........
S........T
18
4 4 r
5 4 r
6 7 r
7 7 r
3 4 b
3 5 b
3 6 b
3 7 b
3 8 b
3 9 b
3 10 b
5 1 b
5 2 b
5 3 b
5 4 b
5 5 b
5 6 b
5 7 b

output:

14

result:

ok single line: '14'

Test #4:

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

input:

2 5
.T.#S
....U
10
1 3 b
1 1 r
1 1 b
2 1 r
1 2 b
1 5 b
2 2 r
1 2 r
2 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

5 5
U.S..
.....
.....
.....
.T...
12
2 4 b
4 1 b
2 2 r
1 5 b
2 2 b
4 3 b
5 3 r
1 2 b
3 2 r
2 1 r
3 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 13836kb

input:

5 4
....
...U
....
S#..
.#T.
12
3 4 b
2 1 b
4 3 r
2 2 b
4 3 b
3 3 r
2 3 r
1 1 b
2 2 r
4 4 b
3 1 b
1 3 r

output:

7

result:

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