QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#188609 | #5489. Room Evacuation | Gamal74 | AC ✓ | 4601ms | 38220kb | C++20 | 4.8kb | 2023-09-26 04:17:15 | 2023-09-26 04:17:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define fi first
#define se second
#define pp push_back
#define all(x) (x).begin(), (x).end()
#define Ones(n) __builtin_popcount(n)
#define endl '\n'
#define mem(arrr, xx) memset(arrr,xx,sizeof arrr)
//#define int long long
#define debug(x) cout << (#x) << " = " << x << endl
void Gamal() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef Clion
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
int dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};
const double EPS = 1e-9;
const ll OO = 0X3F3F3F3F3F3F3F3F;
const int N = 20 + 5, INF = INT_MAX, MOD = 1e9 + 7, LOG = 20;
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;
int conv(int i, int j, int t) {
return i * m + j + t * (n * m);
}
char arr[N][N];
void solve() {
int t;
cin >> n >> m >> t;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> arr[i][j];
}
}
int src = 2 * n * m * (t + 1), sink = src + 1;
PushRelabel flw(sink + 1);
// handle src to nodes
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] == 'P') {
flw.addEdge(src, 2 * conv(i, j, 0), 1);
}
}
}
// handle nodes to sink
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] == 'E') {
for (int k = 0; k <= t; ++k) {
flw.addEdge(2 * conv(i, j, k) + 1, sink, 1);
}
}
}
}
// handle nodes to nodes
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] == '#')continue;
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (ni < 0 || ni >= n || nj < 0 || nj >= m || arr[ni][nj] == '#')continue;
for (int l = 0; l < t; ++l) {
flw.addEdge(2 * conv(i, j, l) + 1, 2 * conv(ni, nj, l + 1), 1);
}
}
}
}
// handle nodes it itself
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] == '#')continue;
for (int k = 0; k <= t; ++k) {
flw.addEdge(2 * conv(i, j, k), 2 * conv(i, j, k) + 1, 1);
if (k != t)flw.addEdge(2 * conv(i, j, k) + 1, 2 * conv(i, j, k + 1), 1);
}
}
}
cout << flw.max_flow(src, sink);
}
signed main() {
Gamal();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3656kb
input:
4 5 3 ..... ..P#. ..PPE ..P.E
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
3 3 5 ... P#P P#E
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 3ms
memory: 8580kb
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:
89
result:
ok single line: '89'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
4 1 137 . # # #
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 11ms
memory: 16596kb
input:
14 15 200 ###EPP...#P.E#. #.##EE#E#.PP#EP E.PPP.E.#.##PPP #E.EP##..#.#.## #P###EEE#E#.E.# #P..EPPE.#..EE. PP#P#E.#PP#.PEE .PEPEE#E#..EE## EE.PP.PE#EE#PEE P.#EPP.P#.EPP#P E..#P#PEPE#EP.# E.PP.EEP.#.#... PP.P#P#PEEEPPEE #.EE.P.P.P#E#PP
output:
55
result:
ok single line: '55'
Test #6:
score: 0
Accepted
time: 7ms
memory: 11284kb
input:
9 13 200 .P.E.PEPP.EE. .##E.EEE.P##. P##P.#..EP.PE E#EE...PEEP.E E#....PPP#PPP .#EPE.PPEP#PE #EEP...##P... #.P##EE#.E#.# #EPEP.E...P#P
output:
28
result:
ok single line: '28'
Test #7:
score: 0
Accepted
time: 0ms
memory: 4552kb
input:
8 13 28 #PEPP..#E##EE EEEPPEPPE...E ..##PP#P#..#P P...PP.PPE#PE EPPEE.PEEPEPP #EPE#PE#####. P.P#.#E.E...P ###EP##.EP.PP
output:
32
result:
ok single line: '32'
Test #8:
score: 0
Accepted
time: 15ms
memory: 19028kb
input:
13 20 200 P.E#E.P.#EP#P..#.#E. ..E...P.E#EP#.PE.P#P ##E#E#.P##P#PP#E#.EE .P#..#PP.E#E#E#E#PPP #P.EEP..EPEPPEE..E#E PPE###...EEPP#.#E.P. PP.#.##E#E##.P#.EPEE EE#PEPE.#P#.PP..EP.. #.EPE#P.P#EEPPP.P##. ####E###PE#E###EPP## #.EP#.#..#.#E#..#EPE E#EEPE#PP..EPE#P#P.E .#PPE#E.#.#EP#PEE#EP
output:
62
result:
ok single line: '62'
Test #9:
score: 0
Accepted
time: 3ms
memory: 8188kb
input:
14 5 200 ..EEP P.EE# P.### ##.P# #E.EP ...EE ..#.. PEPEE ##.PP .P..P PP#.. E.#EP E..P. EE.E.
output:
15
result:
ok single line: '15'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
2 1 167 P P
output:
0
result:
ok single line: '0'
Test #11:
score: 0
Accepted
time: 8ms
memory: 11492kb
input:
6 20 199 .PE.E...P.EE...PE.## P.EPP.#EE#E.PP.P.#.E E#E#E#EEP.P.E##P.E## EEP#PPE#EE..##PPPP.# .PE#EP.PE#EE.P.E#EE# E#P.EEPP..P..EE#E#.P
output:
28
result:
ok single line: '28'
Test #12:
score: 0
Accepted
time: 0ms
memory: 9512kb
input:
7 14 200 #PE##..E#EPEP. ###PEPE.P##PPP #E.#P#PE###.PP .E#E.#EEE#PP.P P..EEEE#.E#P.. #.E.P.PEPEPP#. #E#EEPE.E##...
output:
24
result:
ok single line: '24'
Test #13:
score: 0
Accepted
time: 7ms
memory: 12356kb
input:
8 19 200 .#..#P#.P.P#.E.E..P #P.P##..P.PPE.EEP.E #.##P.#E.P##P....E. ##EPEE#.EPP.#.#EE.# #.#EPP#EE.PP#E#E.## PPPPE.EEP#EPEE..PPP #P###PPP#EP..E#P### #.##EPPE.#.PE#E.##P
output:
35
result:
ok single line: '35'
Test #14:
score: 0
Accepted
time: 3ms
memory: 8920kb
input:
6 15 200 ##.#P.E#.PE.P.E PEE...#E#EE..#P P.#.E#EP..#E... .EE#P#E.P.PEP#P P###.EE#P##.EEE .PE.E.P.PP.###.
output:
18
result:
ok single line: '18'
Test #15:
score: 0
Accepted
time: 17ms
memory: 4640kb
input:
2 12 200 #P.E.#...P#E E.##..#.P#..
output:
1
result:
ok single line: '1'
Test #16:
score: 0
Accepted
time: 1ms
memory: 4008kb
input:
12 1 108 # # E P # . P # E # # .
output:
1
result:
ok single line: '1'
Test #17:
score: 0
Accepted
time: 16ms
memory: 14488kb
input:
17 15 126 #.##.EEPEP#EE.E #P.#PPEP.P.#E#P .P##E.##E.E##PE P#EPPE.E#.#EPE# .EEEE.P#E.P...# P.PEPPEEEE.PE#. .#E..#...PPPPEP ##.EE.PE.PPPPE. P..P#PEP....P#E P.#...P...EPEE. #EE#.EPP#PEE.## .#.##.P#PEE###E #.#..#..EE.E#.P .#.#E.PPEEE...P PPPP##.E.PPEEPP EE.EE.EP.PEPPP. EP#P.P.P.#.P.P.
output:
67
result:
ok single line: '67'
Test #18:
score: 0
Accepted
time: 4601ms
memory: 38180kb
input:
20 20 200 PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPP...
output:
200
result:
ok single line: '200'
Test #19:
score: 0
Accepted
time: 4204ms
memory: 38048kb
input:
20 20 200 E#PPPPPPPPPPPPPPPPPP ##PPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPP...
output:
0
result:
ok single line: '0'
Test #20:
score: 0
Accepted
time: 4ms
memory: 9948kb
input:
20 20 38 E................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .....................
output:
1
result:
ok single line: '1'
Test #21:
score: 0
Accepted
time: 7ms
memory: 9636kb
input:
20 20 37 E................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .....................
output:
0
result:
ok single line: '0'
Test #22:
score: 0
Accepted
time: 4ms
memory: 4780kb
input:
20 20 5 .....EEEEEEEEEE..... PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPPPPPP .................... .................... .................... .................... .................... .................... .................... ......................
output:
50
result:
ok single line: '50'
Test #23:
score: 0
Accepted
time: 943ms
memory: 18560kb
input:
20 20 200 P#PPP#PPP#PPP#PPP#E# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P#P# P#P#P#P#P#P#P#P#P...
output:
200
result:
ok single line: '200'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
3 5 2 P.E.P ..... ..P..
output:
1
result:
ok single line: '1'
Test #25:
score: 0
Accepted
time: 1ms
memory: 3748kb
input:
1 20 19 P..................E
output:
1
result:
ok single line: '1'
Test #26:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
6 9 6 ..E...E.. ......... ##.###.## #P...P.P# #.P...PP# #########
output:
6
result:
ok single line: '6'
Test #27:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
6 9 6 ..E...E.. ......... ##.###.## #P.P...P# #PP...P.# #########
output:
6
result:
ok single line: '6'
Test #28:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
9 6 6 ####.. #.P#.. #P...E #..#.. #..#.. #.P#.. #P...E #PP#.. ####..
output:
6
result:
ok single line: '6'
Test #29:
score: 0
Accepted
time: 1ms
memory: 3924kb
input:
9 6 6 ####.. #PP#.. #P...E #.P#.. #..#.. #..#.. #P...E #.P#.. ####..
output:
6
result:
ok single line: '6'
Test #30:
score: 0
Accepted
time: 1ms
memory: 3736kb
input:
6 9 6 ######### #PP...P.# #P.P...P# ##.###.## ......... ..E...E..
output:
6
result:
ok single line: '6'
Test #31:
score: 0
Accepted
time: 1ms
memory: 3732kb
input:
6 9 6 ######### #.P...PP# #P...P.P# ##.###.## ......... ..E...E..
output:
6
result:
ok single line: '6'
Test #32:
score: 0
Accepted
time: 1ms
memory: 3724kb
input:
9 6 6 ..#### ..#PP# E...P# ..#P.# ..#..# ..#..# E...P# ..#P.# ..####
output:
6
result:
ok single line: '6'
Test #33:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
9 6 6 ..#### ..#P.# E...P# ..#..# ..#..# ..#P.# E...P# ..#PP# ..####
output:
6
result:
ok single line: '6'
Test #34:
score: 0
Accepted
time: 25ms
memory: 18652kb
input:
20 20 200 P#...#...#...#...#E# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#.#.# .#.#.#.#.#.#.#.#....
output:
0
result:
ok single line: '0'
Test #35:
score: 0
Accepted
time: 70ms
memory: 38220kb
input:
20 20 200 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... ....................
output:
0
result:
ok single line: '0'
Test #36:
score: 0
Accepted
time: 0ms
memory: 5232kb
input:
20 20 14 ..###P#.EP......#... P#P.#.P.#......E.#P. .##P#......#.#.#.... ..PP#P...#....##.... P..P..PP..EE.#..#.#. #P#....PPP#P.P..E##. .###PPP.#.E.P.E.PP.. #PP.PP...EPP##PP.P.. ..##.P..#P##.E.##P#E E.....P...#P.#.#..#. ....#.#..#..EP.#.P.. ....EPEE#.....E..... .P..#..P.E...P.PEP#. P..EP.##E...#.P.##...
output:
75
result:
ok single line: '75'
Test #37:
score: 0
Accepted
time: 6ms
memory: 6752kb
input:
20 20 23 ...P..PEPPPP.PPEP.P# E..P.##.E.P##EPPP.P. E.P..EP.P.#P..P#P.#P .E.P....###EP.#PPPEP EE#P.P.P.PPP.#..P#PP EP..##PP...PP.P..P.E #E.E.#P.#.#..E#..PPP EP#PPE##.P.PE.PPP.P. P.PEPP.#...P#P.PPP.E .#P.E.PP.PP...P.P.P. #...#P#P##P#E#P.#... #P#PP.PP#..PEPP.#PPP P#.P.P##PP.#.P##P#P# ..#.#E.#E....PP......
output:
148
result:
ok single line: '148'
Test #38:
score: 0
Accepted
time: 0ms
memory: 4960kb
input:
20 20 10 #.P.....##....#.PPP. #..PP...PP.#........ #..#.......#..#...## .PE..P..........#..# P##P....E.....E#.... P............#..#... .E#..............P.. ..........#..P....#. ###.E#.##P..#....E#P E.#.#...#.##..E..P.. P#............P..#.# .PPP###...P......... ..#...P.....#...#.#. ...#.E..P..#P##..#...
output:
38
result:
ok single line: '38'
Test #39:
score: 0
Accepted
time: 2ms
memory: 5012kb
input:
20 20 10 E..P...E..P.#.P....# .........#.......#.E E......EP..#........ .P...#.#.#.PP.....E. ........P#.......##. ..#.##.#.P#...E#P### ....#..#.P#E...PP##. ....P#...E......#..# .#.P......#P.E...... ....E.......E......P .#.E...........#P#E# ......#...PP.....##. ..#......#...E##.#.. ..E...#...#......P...
output:
36
result:
ok single line: '36'
Test #40:
score: 0
Accepted
time: 0ms
memory: 4908kb
input:
20 20 10 ..E...P##.P#.E....#. .P...E#....P......#. P.#.#..#.....E...E.. .##......P.E........ #..P#.P..#.P...E.P.. ...P..........#..#.. .E...E.........P#.P# ....#..............# .#....E..#....#.P.#. ..#....P..#.....P... ...E...#E...#.#..... #...P.#..#......#### .#..P....P.P#...PP#E ..#......P#P...P.....
output:
33
result:
ok single line: '33'
Test #41:
score: 0
Accepted
time: 3ms
memory: 4956kb
input:
20 20 10 ....P.E.....#P...... .....E###.P.#P..#... .#.P.P..P....P...##P ..........P#E#..#..# #...E..#..........EP .E.P..#P...P#...E.#. ........#.#.P...PEE. .....P.....##..#E... ....P.....PE#....P#E .P.##..E.........#.P .#.....#E....#...#.P #..#..PE.#..P..#...# .....P....#.##..PP.. .................#...
output:
39
result:
ok single line: '39'
Test #42:
score: 0
Accepted
time: 2ms
memory: 4704kb
input:
20 20 10 ....#...P##.......P. ..P#E.#.P#.E....#... ....#..PPP.........# ....E#..P#....P#.... ...........#..##.#.. .....P........E#P... .........#P..E...... ..###.#.P##.....E#.# .#..P#......PE#....# #...##........#...#E .##.#..#P#..#.E..#.P E.#........#...P.... ..E##EP....PP#PP...# ....#..##P...#P..P...
output:
33
result:
ok single line: '33'
Test #43:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
1 1 200 E
output:
0
result:
ok single line: '0'
Test #44:
score: 0
Accepted
time: 1ms
memory: 3544kb
input:
1 1 200 P
output:
0
result:
ok single line: '0'
Test #45:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
1 1 200 .
output:
0
result:
ok single line: '0'
Test #46:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
1 1 200 #
output:
0
result:
ok single line: '0'
Test #47:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
1 2 1 PE
output:
1
result:
ok single line: '1'
Test #48:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
1 3 1 P.E
output:
0
result:
ok single line: '0'
Test #49:
score: 0
Accepted
time: 1ms
memory: 3724kb
input:
20 1 19 P . . . . . . . . . . . . . . . . . . E
output:
1
result:
ok single line: '1'