Maximum Flow

  Here, I would like to use Ford-Fulkerson Method to determine the Maximum Flow of a given directed graph in the USACO training problem Drainage Ditches:

 1 import java.util.*;
 2 import java.io.*;
 3 
 4 public class ditch {
 5     public static final int INF = (1<<30);
 6     public static Scanner in;
 7     public static PrintWriter out;
 8     public static int num;        // number of vertices
 9     public static int[][] adjMat;    // residual matrix
10     public static int[] path;        // record of augmented path
11     public static int len;        // length of augmented path
12     
13     public static int maxFlow() {
14         // Implementation of the Ford-Fulkerson Method
15         int val = 0;
16         while (genAugPath()) {
17             // Whenever an augmented path is discovered
18             //        the capacity of it will be added to the flow
19             int min = INF;
20             for (int i=0;i<len-1;i++) {
21                 // Determine the capacity of the path
22                 if (adjMat[path[i+1]][path[i]]<min)
23                     min = adjMat[path[i+1]][path[i]];
24             }
25             for (int i=0;i<len-1;i++) {
26                 // Modify the residual matrix
27                 adjMat[path[i+1]][path[i]]-=min;
28                 adjMat[path[i]][path[i+1]]+=min;
29             }
30             val += min;
31         }
32         return val;
33     }
34     public static boolean genAugPath() {
35         // Breadth-First Search for an augmented path
36         //        return whether a path has been found
37         boolean[] vis = new boolean[num];
38         Queue<Integer> q= new LinkedList<Integer>();
39         int[] prev = new int[num];
40         vis[0] = true;
41         q.add(new Integer(0));
42         prev[0] = -1;
43         while (!q.isEmpty()){
44             int k = q.poll().intValue();
45             for (int i=1;i<num-1;i++) {
46                 if (!vis[i]&&adjMat[k][i]>0) {
47                     vis[i] = true;
48                     q.add(new Integer(i));
49                     prev[i] = k;
50                 }
51             }
52             if (adjMat[k][num-1]>0) {
53                 // When destination is reached,
54                 //        an augmented path is found
55                 len = 0;
56                 path[len++] = num-1;
57                 int idx = k;
58                 while (idx>=0) {
59                     path[len++] = idx;
60                     idx = prev[idx];
61                 }
62                 return true;
63             }
64         }
65         return false;
66     }
67     public static void main(String[] args) throws IOException{
68         in = new Scanner(new FileReader("ditch.in"));
69         int n = in.nextInt();
70         num = in.nextInt();
71         adjMat = new int[num][num];
72         for (int i=0;i<n;i++) {
73             adjMat[in.nextInt()-1][in.nextInt()-1] += in.nextInt();
74         }
75         in.close();
76         out = new PrintWriter(new FileWriter("ditch.out"));
77         path = new int[num];
78         out.println(maxFlow());
79         out.close();
80     }
81 }
原文地址:https://www.cnblogs.com/DevinZ/p/4411437.html