Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3866 [TJOI2009] 战争游戏

P3866 [TJOI2009] 战争游戏

题意即将敌军的位置和边界的位置割开,因为割点,所以拆点即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define inf INT_MAX
using namespace std;
const int N = 2e3 + 10, M = 3e4 + 10;
const int dx[4] = { 1, 0, 0, -1 }, dy[4] = { 0, 1, -1, 0 };
int n, m, st, ed, d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
for (int i = st << 1 | 1; i <= ed << 1; i++)
d[i] = -1;
d[st << 1 | 1] = 0;
cur[st << 1 | 1] = hd[st << 1 | 1];
queue <int> q;
q.push(st << 1 | 1);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed << 1)
return true;
q.push(edg[i]);
}
}
return false;
}
int exploit(int x, int limit)
{
if (x == ed << 1)
return limit;
int res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
int t = exploit(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = exploit(st << 1 | 1, inf))
res += flow;
return res;
}
void add (int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
int num (int x, int y)
{
return (x - 1) * m + y;
}
bool inside (int x, int y)
{
return x > 0 && x <= n && y > 0 && y <= m;
}
int main ()
{
cin >> n >> m;
st = 0;
ed = num(n, m) + 1;
for (int i = st << 1 | 1; i <= ed << 1; i++)
hd[i] = -1;
for (int i = 1, a; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> a;
int t = num(i, j);
if (a == -1)
continue;
if (!a)
{
add(st << 1 | 1, t << 1, inf);
add(t << 1, st << 1 | 1, 0);
}
add(t << 1, t << 1 | 1, a ? a : inf);
add(t << 1 | 1, t << 1, 0);
for (int k = 0; k < 4; k++)
{
int nx = i + dx[k],
ny = j + dy[k];
int h = num(nx, ny);
if (!inside(nx, ny))
continue;
add(t << 1 | 1, h << 1, inf);
add(h << 1, t << 1 | 1, 0);
}
if (i == 1 || j == 1 || i == n || j == m)
{
add(t << 1 | 1, ed << 1, inf);
add(ed << 1, t << 1 | 1, 0);
}
}
cout << dinic();
return 0;
}