# [TJOI2007]脫險【最大流】

### 題目鏈接

首先，每個時刻，每個點最多經過一個人，那麼來說，我們需要對於每個時刻的每個點進行拆點，限制流量。

然後，因爲圖實際上只有10*10，因爲最外圍是包圍圈，然後時間上其實是上限50，所以我們不妨對於時間來建點，表示時間爲Tim時候的座標爲（x，y）的點，那麼，根據每一秒移動一步，初始爲0，我們向四個方向都是鏈接上下一秒時刻對應的點值。

接下去，就是最大流的部分了。

``````#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int dir[4][2] =
{
-1, 0,
0, -1,
0, 1,
1, 0
};
const int maxN = 2e4 + 7, maxM = 2e5 + 7;
int R, C, Tm;
inline bool In_Map(int x, int y) { return x >= 0 && y >= 0 && x < R && y < C; }
struct Eddge
{
int nex, to; ll flow;
Eddge(int a=-1, int b=0, ll c=0):nex(a), to(b), flow(c) {}
}edge[maxM];
inline void addEddge(int u, int v, ll w)
{
}
struct Max_Flow
{
int gap[maxN], d[maxN], que[maxN], ql, qr, S, T, cur[maxN], node;
inline void init()
{
for(int i=0; i<=node + 1; i++)
{
gap[i] = d[i] = 0;
}
++gap[d[T] = 1];
que[ql = qr = 1] = T;
while(ql <= qr)
{
int x = que[ql ++];
{
v = edge[i].to;
if(!d[v]) { ++gap[d[v] = d[x] + 1]; que[++qr] = v; }
}
}
}
inline ll aug(int x, ll FLOW)
{
if(x == T) return FLOW;
int flow = 0;
for(int &i=cur[x], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(d[x] == d[v] + 1)
{
ll tmp = aug(v, min(FLOW, edge[i].flow));
flow += tmp; FLOW -= tmp; edge[i].flow -= tmp; edge[i ^ 1].flow += tmp;
if(!FLOW) return flow;
}
}
if(!(--gap[d[x]])) d[S] = node + 1;
return flow;
}
inline ll max_flow()
{
init();
ll ret = aug(S, INF);
while(d[S] <= node) ret += aug(S, INF);
return ret;
}
} mf;
int tot, ID[15][15];
char mp[15][15];
int main()
{
scanf("%d%d%d", &R, &C, &Tm);
for(int i=0; i<R; i++)
{
scanf("%s", mp[i]);
for(int j=0; j<C; j++)
{
if(mp[i][j] == '*') continue;
ID[i][j] = ++tot;
}
}
mf.S = 0; mf.T = 2 * (Tm + 1) * tot + 1; mf.node = mf.T + 1;
for(int i=0; i<=mf.node + 1; i++) head[i] = -1;
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
if(mp[i][j] == 'P')
{
}
else if(mp[i][j] == 'O')
{
}
}
}
for(int tim = 0; tim < Tm; tim++)
{
for(int i=0, x, y; i<R; i++)
{
for(int j=0; j<C; j++)
{
if(mp[i][j] == '*' || mp[i][j] == 'O') continue;
_add(tim * tot * 2 + ID[i][j], tim * tot * 2 + tot + ID[i][j], 1);  //capa limited
_add(tim * tot * 2 + tot + ID[i][j], (tim + 1) * tot * 2 + ID[i][j], 1);    //stay here
for(int k=0; k<4; k++)
{
x = i + dir[k][0]; y = j + dir[k][1];
if(!In_Map(x, y) || mp[x][y] == '*') continue;
if(mp[x][y] == 'O')
{
_add(tim * tot * 2 + tot + ID[i][j], ID[x][y], 1);  //move to next position
}
else _add(tim * tot * 2 + tot + ID[i][j], (tim + 1) * tot * 2 + ID[x][y], 1);
}
}
}
}
printf("%lld\n", mf.max_flow());
return 0;
}
``````