QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153432 | #5472. Secure the Top Secret | ideograph_advantage | WA | 3ms | 14096kb | C++17 | 8.8kb | 2023-08-30 01:46:28 | 2023-08-30 01:46:29 |
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 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);
for(int i = 0; i < n; i++){
for(auto j : G[i]){
if(j.flow > 0) debug("edge", j.from, j.to, j.flow);
}
}
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, SZ(G[b])});
G[b].pb(edge{b, a, 0, 0, -cost, SZ(G[a]) - 1});
}
} flow;
#define X ff
#define Y ss
struct border{
pii A, B, cell;
int side;
};
bool operator==(border a, border b){
return a.A == b.A && a.B == b.B && a.cell == b.cell && a.side == b.side;
}
ostream& operator<<(ostream& o, border b){
return o << '(' << b.A << ',' << b.B << ',' << b.cell << ',' << b.side << ')';
}
pair<pii, pii> get_edge(int x, int y, int side){
if(side == 0) return {mp(x, y), mp(x - 1, y)};
if(side == 1) return {mp(x - 1, y), mp(x - 1, y - 1)};
if(side == 2) return {mp(x - 1, y - 1), mp(x, y - 1)};
if(side == 3) return {mp(x, y - 1), mp(x, y)};
assert(false);
}
pii get_adj(int x, int y, int side){
if(side == 0) return {x, y + 1};
if(side == 1) return {x - 1, y};
if(side == 2) return {x, y - 1};
if(side == 3) return {x + 1, y};
assert(false);
}
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) + 4, 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;
vector<string> s(n + 2);
s[0] = s[n + 1] = string(m + 2, '#');
for(int i = 1; i <= n; i++){
cin >> s[i];
s[i] = "#" + s[i] + "#";
for(int j = 1; j <= m; j++){
if(s[i][j] == 'S') S = mp(i, j);
if(s[i][j] == 'T') T = mp(i, j);
if(s[i][j] == 'U') U = mp(i, j);
}
}
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<vector<int>> vst(n + 2, vector<int>(m + 2));
vector<pii> dir4 = {mp(0, 1), mp(1, 0), mp(-1, 0), mp(0, -1)};
vector<pii> dir8;
for(int i = -1; i <= 1; i++) for(int j = -1; j <= 1; j++) if(i != 0 || j != 0) dir8.pb(mp(i, j));
auto bfs = [&](int sx, int sy, int t, vector<pii> &dir){
queue<pii> q;
q.push(mp(sx, sy));
vst[sx][sy] = t;
while(!q.empty()){
auto [x, y] = q.front();
q.pop();
for(auto [dx, dy] : dir){
int nx = x + dx, ny = y + dy;
if(nx < 0 || nx > n + 1 || ny < 0 || ny > m + 1) continue;
if(vst[nx][ny]) continue;
if(t == 1 && s[nx][ny] == '#') continue;
vst[nx][ny] = t;
q.push(mp(nx, ny));
}
}
};
bfs(S.X, S.Y, 1, dir4);
for(int i = 0; i <= n + 1; i++) pary(iter(vst[i]));
bfs(0, 0, -1, dir8);
for(int i = 0; i <= n + 1; i++) pary(iter(vst[i]));
if(vst[U.X][U.Y] != 1){
cout << "-1\n";
return 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vst[i][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);
}
}
vector<border> bor;
{
int side;
if(T.X == 1) side = 1;
else if(T.X == n) side = 3;
else if(T.Y == 1) side = 2;
else side = 0;
auto [A, B] = get_edge(T.X, T.Y, side);
bor.pb(border({A, B, T, side}));
}
int lst = 0;
char c1 = 'T';
int s1 = (n + 1) * (m + 1);
int s2 = s1 + 1;
int t1 = s2 + 1;
int t2 = t1 + 1;
bool SU = false, UT = false;
auto zuoshi = [&](int nxt, char c2, int to){
//debug("check zuoshi", lst, c2, SZ(bor));
if(c2 == '.') return;
debug("zuoshi", lst, nxt, c1, c2);
if(mp(c1, c2) == mp('S', 'U') ||
mp(c1, c2) == mp('U', 'S')){
if(SU){
debug("bad SU");
cout << "-1\n";
exit(0);
}
debug("OK SU");
for(int i = lst; i < to; i++){
auto [A, B, cell, side] = bor[i];
//debug(bor[i]);
flow.add_edge(s1, A.X * (m + 1) + A.Y, 2, 0);
flow.add_edge(s1, B.X * (m + 1) + B.Y, 2, 0);
}
SU = true;
}
else if(mp(c1, c2) == mp('U', 'T') ||
mp(c1, c2) == mp('T', 'U')){
if(UT){
debug("bad UT");
cout << "-1\n";
exit(0);
}
debug("OK UT");
for(int i = lst; i < to; i++){
auto [A, B, cell, side] = bor[i];
//debug(bor[i]);
flow.add_edge(A.X * (m + 1) + A.Y, t1, 2, 0);
flow.add_edge(B.X * (m + 1) + B.Y, t1, 2, 0);
}
UT = true;
}
lst = nxt;
c1 = c2;
//debug("ok zuoshi");
};
while(true){
auto [A, B, cell, side] = bor.back();
debug("border", A, B, cell, side);
//pary(iter(bor));
zuoshi(SZ(bor), s[cell.X][cell.Y], SZ(bor) - 1);
if(SZ(bor) > 1 && bor.front() == bor.back()) break;
auto [tx, ty] = get_adj(cell.X, cell.Y, (side + 1) % 4);
if(vst[tx][ty] == -1){
auto [nA, nB] = get_edge(cell.X, cell.Y, (side + 1) % 4);
bor.pb(border({nA, nB, cell, (side + 1) % 4}));
continue;
}
auto [nx, ny] = get_adj(tx, ty, side);
if(vst[nx][ny] == -1){
auto [nA, nB] = get_edge(tx, ty, side);
bor.pb(border({nA, nB, mp(tx, ty), side}));
continue;
}
zuoshi(SZ(bor), s[tx][ty], SZ(bor));
auto [nA, nB] = get_edge(nx, ny, (side - 1 + 4) % 4);
bor.pb(border({nA, nB, mp(nx, ny), (side - 1 + 4) % 4}));
}
//debug("ok");
flow.add_edge(s2, s1, 2, 0);
flow.add_edge(t1, t2, 2, 0);
debug("st", s1, s2, t1, t2);
ll ans = MAX;
if(flow.MinCostMaxFlow(s2, t2, ans) < 2) cout << "-1\n";
else cout << ans << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 12096kb
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: 14064kb
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: 1ms
memory: 13860kb
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: 2ms
memory: 9744kb
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: 3ms
memory: 11796kb
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: 0
Accepted
time: 0ms
memory: 11796kb
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:
-1
result:
ok single line: '-1'
Test #7:
score: 0
Accepted
time: 2ms
memory: 11852kb
input:
3 3 UST ### .#. 2 1 1 r 1 2 r
output:
-1
result:
ok single line: '-1'
Test #8:
score: 0
Accepted
time: 1ms
memory: 13908kb
input:
4 2 U. .. .T S. 8 1 1 b 3 1 r 4 1 r 1 1 r 2 2 b 3 2 b 3 1 b 2 1 r
output:
5
result:
ok single line: '5'
Test #9:
score: 0
Accepted
time: 0ms
memory: 13792kb
input:
2 2 S. UT 4 1 1 b 1 2 b 2 1 r 1 1 r
output:
-1
result:
ok single line: '-1'
Test #10:
score: 0
Accepted
time: 0ms
memory: 14096kb
input:
3 4 #U.T #... .S#. 3 1 2 r 2 2 b 3 1 r
output:
-1
result:
ok single line: '-1'
Test #11:
score: -100
Wrong Answer
time: 2ms
memory: 13840kb
input:
3 5 .U.S. .#.## #.T#. 7 1 3 r 1 2 r 1 4 r 1 1 b 1 3 b 2 3 b 3 2 r
output:
3
result:
wrong answer 1st lines differ - expected: '-1', found: '3'