QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#152301 | #5472. Secure the Top Secret | ideograph_advantage | ML | 3ms | 13700kb | C++17 | 5.1kb | 2023-08-27 23:33:02 | 2023-08-27 23:33:03 |
Judging History
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 zisk
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(nx != 1 && ny != 1 && nx != n && ny != 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";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11588kb
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: 1ms
memory: 13700kb
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: 3ms
memory: 11652kb
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: -100
Memory Limit Exceeded
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