QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#544424 | #9224. Express Eviction | Fork512Hz | RE | 4ms | 4148kb | C++20 | 3.5kb | 2024-09-02 16:28:17 | 2024-09-02 16:28:17 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<utility>
#include<vector>
#include<functional>
#include<queue>
#include<set>
#include<cmath>
using namespace std;
typedef pair<long long, long long> pii;
typedef vector<long long> vll;
typedef long long ll;
typedef long long i64;
//#define DEBUG
#ifdef DEBUG
const long long N = 31000;
#endif
#ifndef DEBUG
const long long N = 31000;
#endif
// const ll M = 998244353;
ll n, m;
const int INF = 0x7fffffff;
const int IN = 10000000;
struct MF {
struct edge {
int v, nxt, cap, flow;
} e[N];
int fir[N], cnt = 0;
int n, S, T;
ll maxflow = 0;
int dep[N], cur[N];
void init() {
memset(fir, -1, sizeof fir);
cnt = 0;
}
void addedge(int u, int v, int w) {
// cout << u << ' ' << v << ' ' << w << ' ' << cnt << '\n';
e[cnt] = {v, fir[u], w, 0};
fir[u] = cnt++;
e[cnt] = {u, fir[v], 0, 0};
fir[v] = cnt++;
}
bool bfs() {
queue<int> q;
memset(dep, 0, sizeof(int) * (n + 1));
dep[S] = 1;
q.push(S);
while (q.size()) {
int u = q.front();
q.pop();
for (int i = fir[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if ((!dep[v]) && (e[i].cap > e[i].flow)) {
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
return dep[T];
}
int dfs(int u, int flow) {
if ((u == T) || (!flow)) return flow;
int ret = 0;
for (int& i = cur[u]; ~i; i = e[i].nxt) {
int v = e[i].v, d;
if ((dep[v] == dep[u] + 1) &&
(d = dfs(v, min(flow - ret, e[i].cap - e[i].flow)))) {
ret += d;
e[i].flow += d;
e[i ^ 1].flow -= d;
if (ret == flow) return ret;
}
}
return ret;
}
void dinic() {
while (bfs()) {
memcpy(cur, fir, sizeof(int) * (n + 1));
maxflow += dfs(S, INF);
}
}
};
string s[55];
MF maxflow;
inline ll inid(ll x, ll y)
{
return x * m + y;
}
inline ll outid(ll x, ll y)
{
return x * m + y + n*m;
}
int main()
{
#ifdef DEBUG
freopen("1.txt", "r", stdin);
#endif
#ifndef DEBUG
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#endif
cin >> n >>m;
const ll SRC = 2*n*m;
const ll DST = SRC + 1;
maxflow.init();
maxflow.n = n*m*2 + 2;
maxflow.S = SRC;
maxflow.T = DST;
for(ll i=0; i<n; i++)
cin >> s[i];
for(ll i=0; i<n; i++)
for(ll j=0; j<m; j++) if(s[i][j] == '#')
{
maxflow.addedge(inid(i, j), outid(i, j), 1);
if(j == 0 || i == n-1) // Bottom left
maxflow.addedge(SRC, inid(i, j), IN);
if(j == m-1 || i == 0) // Top right
maxflow.addedge(outid(i, j), DST, IN);
for(ll di=0; di<=2; di++) // Search resident pairs in 5*5
{
if(i - di < 0) break;
for(ll dj=(di==0? 1: -2); dj<=2; dj++)
{
if(j - dj < 0) break;
if(j - dj >= m) continue;
if(s[i-di][j-dj] == '#')
{
maxflow.addedge(outid(i, j), inid(i-di, j-dj), IN);
maxflow.addedge(outid(i-di, j-dj), inid(i, j), IN);
}
}
}
}
maxflow.dinic();
cout << maxflow.maxflow << '\n';
return 0;
}
/*BEFORE SUBMISSION:
1. remove #define DEBUG
2. check data constraints
3. check memory reset for multitest
4. check integer overflow
5. parenthesize all bitwise operations
6. run all sample tests in random order
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3704kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3936kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
11
result:
ok 1 number(s): "11"
Test #3:
score: 0
Accepted
time: 4ms
memory: 4148kb
input:
35 35 ....###########...#########........ ##..#######################..#####. ....#######################..#####. ...#.....##################..#####. .##......##################.....##. .##..##..#############.....#....##. .....##..#############......##..##. ....#....#############..##..##..##. ####.....
output:
16
result:
ok 1 number(s): "16"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
30 20 .#..........##...... ..#......#..##...... .......#..........#. ..#......#..##...... .....#......#....... .#.........#........ ......#...#......... ..#..##..#...#...... ......#.......#..... ..#....#............ ........#..#.#...... ....##..#........... .........#.#.......# ........##.......... ...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
50 45 ...##................##...................#.. ...#......#.#..........#.#.....####....#....# ....#.....#..............#..#..##......##..#. .....#......######........#.............#.... ......##...#...##...##.......#....#..#...##.. ...#..##.....##...###.............#..##.....# ...#......########...
output:
24
result:
ok 1 number(s): "24"
Test #6:
score: 0
Accepted
time: 3ms
memory: 3924kb
input:
50 45 ...#...................#.####................ ...........................#......###.#..##.. ..#.#........##.##....#....#................. .....#.....#.##.#.#.........#................ ...........#.#.#.#.##....#.#.......##.#..#... ...#......#...#####......#...##.##........#.. ............####.....
output:
23
result:
ok 1 number(s): "23"
Test #7:
score: 0
Accepted
time: 0ms
memory: 4112kb
input:
50 50 ##................................................ #################################################. ####.............................................. ####.............................................. ####..############################################ ####......................................
output:
7
result:
ok 1 number(s): "7"
Test #8:
score: -100
Runtime Error
input:
50 50 #.##.##..###...#######.##..#####.#...######.###### .####.##.##.#############.#.#.###.##.###.#.#.###.# ...####.##########.###.####.#.####.#############.. #.#..########.#.#####.#..#.##....##########.#.#### ###.##.####.###.#######..##.#####...##.#######..#. #####.########..########..#######.##.#....