Skip to content

Commit a2e2e76

Browse files
committed
modify code
1 parent d345d14 commit a2e2e76

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/class47/Code03_DinicAlgorithm.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public int maxFlow(int s, int t) {
5757
while (bfs(s, t)) {
5858
Arrays.fill(cur, 0);
5959
flow += dfs(s, t, Integer.MAX_VALUE);
60+
Arrays.fill(depth, 0);
6061
}
6162
return flow;
6263
}
@@ -67,14 +68,16 @@ private boolean bfs(int s, int t) {
6768
boolean[] visited = new boolean[N];
6869
visited[s] = true;
6970
while (!queue.isEmpty()) {
70-
// u是当前节点
7171
int u = queue.pollLast();
7272
for (int i = 0; i < nexts.get(u).size(); i++) {
7373
Edge e = edges.get(nexts.get(u).get(i));
7474
int v = e.to;
7575
if (!visited[v] && e.available > 0) {
7676
visited[v] = true;
7777
depth[v] = depth[u] + 1;
78+
if (v == t) {
79+
break;
80+
}
7881
queue.addFirst(v);
7982
}
8083
}
@@ -90,9 +93,6 @@ private int dfs(int s, int t, int r) {
9093
if (s == t || r == 0) {
9194
return r;
9295
}
93-
if (depth[s] >= depth[t]) {
94-
return 0;
95-
}
9696
int f = 0;
9797
int flow = 0;
9898
// s点从哪条边开始试 -> cur[s]
@@ -112,7 +112,6 @@ private int dfs(int s, int t, int r) {
112112
}
113113
return flow;
114114
}
115-
116115
}
117116

118117
public static void main(String[] args) {

0 commit comments

Comments
 (0)