感觉上边数不太多,加与不加差别不大。
但实际上去掉后快了很多。
求教是我的写法有问题还是当前弧在网格图下确实会比较劣?
代码如下:
class Network {
private:
int next[E], to[E]; ll fc[E];
int head[V], now[V], cnt;
int d[V];
bool bfs(int S, int T) {
memset(d, 0, sizeof(d));
static int que[V];
int l = 0, r = 0;
d[S] = 1;
now[S] = head[S];
que[r++] = S;
while (l != r) {
int x = que[l++];
for (int i = head[x]; i; i = next[i]) {
if (!fc[i] || d[to[i]]) continue;
d[to[i]] = d[x] + 1;
now[to[i]] = head[to[i]];
que[r++] = to[i];
if (to[i] == T) return true;
}
}
return false;
}
ll dfs(int x, int T, ll flow) {
if (x == T) return flow;
ll rest = flow;
for (int &i = now[x]; i && rest; i = next[i]) {
if (!fc[i] || d[to[i]] != d[x] + 1) continue;
ll k = dfs(to[i], T, fc[i] < rest ? fc[i] : rest);
if (!k) d[to[i]] = 0;
rest -= k;
fc[i] -= k;
fc[i ^ 1] += k;
}
return flow - rest;
}
public:
void clear() {
memset(head, 0, sizeof(head));
cnt = 2;
return;
}
void add(int u, int v, ll w) {
next[cnt] = head[u];
to[cnt] = v;
fc[cnt] = w;
head[u] = cnt++;
next[cnt] = head[v];
to[cnt] = u;
fc[cnt] = 0;
head[v] = cnt++;
return;
}
ll max_flow(int S, int T) {
ll maxf = 0;
while (bfs(S, T)) {
ll flow = dfs(S, T, INF);
if (!flow) break;
maxf += flow;
}
return maxf;
}
} G;