[BZOJ 3504][CQOI 2014] 危桥

题目大意

\(n\) 座岛屿(编号从 \(0\) 开始),一些岛屿之间有桥相连,其中一些桥是危桥,危桥只能经过两遍,而一般的桥可无数次经过。有两个人,分别从 \(a_s\)\(b_s\) 出发去 \(a_t\)\(b_t\) 往返 \(a_n\)\(b_n\) 次,求是否可行。多组询问。

\(4 \leqslant n \leqslant 50\)

\(1 \leqslant a_n, \; b_n \leqslant 50\)

题目链接

BZOJ 3504

CodeVS 3721

题解

可通过次数为流量,从源点向两个起点连接容量为要求次数两倍的边(往返),从两个终点向汇点做类似的事情,如果满流,则可能可行。由于可能会有这么的情况:从源点到某一个起点未流过其对应的终点,而是流到了另一个终点,这样有可能误判(若一开始就没有满流,则一定不可行),那我们交换某一组起点与终点,再跑一遍。

代码

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
#include <cstdio>
#include <climits>
#include <queue>
#include <algorithm>
const int MAXN = 55;
struct Edge;
struct Node {
Edge *e, *curr;
int level;
} N[MAXN];
struct Edge {
Node *u, *v;
Edge *next, *rev;
int cap, flow;
Edge(Node *u, Node *v, int cap) : u(u), v(v), cap(cap), flow(0), next(u->e) {}
};
void addEdge(int u, int v, int cap, int k = 1) {
N[u].e = new Edge(&N[u], &N[v], cap);
N[v].e = new Edge(&N[v], &N[u], k ? cap : 0);
N[u].e->rev = N[v].e;
N[v].e->rev = N[u].e;
}
namespace Dinic {
bool makeLevelGraph(Node *s, Node *t, int n) {
for (int i = 0; i < n; i++) N[i].level = 0;
std::queue<Node *> q;
q.push(s);
s->level = 1;
while (!q.empty()) {
Node *u = q.front();
q.pop();
for (Edge *e = u->e; e; e = e->next) {
if (e->cap > e->flow && e->v->level == 0) {
e->v->level = u->level + 1;
if (e->v == t) return true;
q.push(e->v);
}
}
}
return false;
}
int findPath(Node *s, Node *t, int limit = INT_MAX) {
if (s == t) return limit;
for (Edge *&e = s->curr; e; e = e->next) {
if (e->cap > e->flow && e->v->level == s->level + 1) {
int flow = findPath(e->v, t, std::min(limit, e->cap - e->flow));
if (flow > 0) {
e->flow += flow;
e->rev->flow -= flow;
return flow;
}
}
}
return 0;
}
int solve(int s, int t, int n) {
int res = 0;
while (makeLevelGraph(&N[s], &N[t], n)) {
for (int i = 0; i < n; i++) N[i].curr = N[i].e;
int flow;
while ((flow = findPath(&N[s], &N[t])) > 0) res += flow;
}
return res;
}
}
int n;
char mat[MAXN][MAXN];
void build() {
for (int i = 0; i <= n + 1; i++) for (Edge *&e = N[i].e, *next; e; next = e->next, delete e, e = next);
for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) {
if (mat[i][j] == 'N') addEdge(i, j, INT_MAX);
if (mat[i][j] == 'O') addEdge(i, j, 2);
}
}
int main() {
int as, at, an, bs, bt, bn;
while (scanf("%d %d %d %d %d %d %d", &n, &as, &at, &an, &bs, &bt, &bn) != EOF) {
as++, at++, bs++, bt++;
for (int i = 1; i <= n; i++) scanf("%s", mat[i] + 1);
const int s = 0, t = n + 1;
build();
addEdge(s, as, an << 1, 0);
addEdge(s, bs, bn << 1, 0);
addEdge(at, t, an << 1, 0);
addEdge(bt, t, bn << 1, 0);
int maxFlow = Dinic::solve(s, t, t + 1);
if (maxFlow < (an << 1) + (bn << 1)) {
puts("No");
continue;
}
build();
addEdge(s, as, an << 1, 0);
addEdge(s, bt, bn << 1, 0);
addEdge(at, t, an << 1, 0);
addEdge(bs, t, bn << 1, 0);
maxFlow = Dinic::solve(s, t, t + 1);
if (maxFlow == (an << 1) + (bn << 1)) puts("Yes");
else puts("No");
}
return 0;
}