HDU 3549 Flow Problem

Description

最大流问题

Algorithm

V1.0
EdmondsKarp算法
翻译了佳佳写的类,目前不是太懂
V2.0
FordFulkerson算法
依然不是太懂 不过这个是DFS 而上面那个是BFS
参照佳佳的,把日本哥们的也翻译成了类

Code

V2.0

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
class Edge {
  int to; // 终点
  int cap; // 容量
  int rev; // 反向边
  Edge(int a, int b, int c) {
    to = a;
    cap = b;
    rev = c;
  }
}
class FordFulkerson {
  final int INF = Integer.MAX_VALUE / 2;
  boolean[] used;
  ArrayList[] g;
  int n;
  FordFulkerson(int nn) {
    int n = nn;
    used = new boolean[n];
    g = new ArrayList[n];
    for (int i = 0; i < n; i++) {
      g[i] = new ArrayList();
    }
  }
  void addEdge(int from, int to, int cap) {
    g[from].add(new Edge(to, cap, g[to].size()));
    g[to].add(new Edge(from, 0, g[from].size() - 1));
  }
  int dfs(int v, int t, int f) {
    if (v == t) return f;
    used[v] = true;
    for (int i = 0; i < g[v].size(); i++) {
      Edge e = (Edge)g[v].get(i);
      if (!used[e.to] && e.cap > 0) {
        int d = dfs(e.to, t, Math.min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          ((Edge)g[e.to].get(e.rev)).cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  int getMaxFlow(int s, int t) {
    int flow = 0;
    for (;;) {
      Arrays.fill(used, false);
      int f = dfs(s, t, INF);
      if (f == 0) return flow;
      flow += f;
    }
  }
}
public class Main {
  static Scanner cin = new Scanner(System.in);
  static void solve() {
    int n = cin.nextInt();
    int m = cin.nextInt();
    FordFulkerson fordFulkerson = new FordFulkerson(n);
    for (int i = 0; i < m; i++) {
      int x = cin.nextInt() - 1;
      int y = cin.nextInt() - 1;
      int z = cin.nextInt();
      fordFulkerson.addEdge(x, y, z);
    }
    System.out.println(fordFulkerson.getMaxFlow(0, n - 1));
  }
  public static void main(String[] args) {
    int t = cin.nextInt();
    for (int i = 1; i <= t; i++) {
      System.out.printf("Case %d: ", i);
      solve();
    }
  }
}

V1.0

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
class Edge {
  int from;
  int to;
  int cap; //容量
  int flow;
  Edge(int a, int b, int c, int d) {
    from = a;
    to = b;
    cap = c;
    flow = d;
  }
}
class EdmondsKarp {
  final int INF = Integer.MAX_VALUE / 2;
  int m;
  int n;
  ArrayList edges;
  ArrayList[] g;
  int[] a;
  int[] p;
  EdmondsKarp(int nn) {
    n = nn;
    g = new ArrayList[n];
    for (int i = 0; i < n; i++) {
      g[i] = new ArrayList();
    }
    edges = new ArrayList();
    p = new int[n];
  }
  void addEdge(int from, int to, int cap) {
    edges.add(new Edge(from, to, cap, 0));
    edges.add(new Edge(to, from, 0, 0));
    m = edges.size();
    g[from].add(m - 2);
    g[to].add(m - 1);
  }
  int getMaxFlow(int s, int t) {
    int flow = 0;
    for (;;) {
      a = new int[n];
      LinkedList q = new LinkedList();
      a[s] = INF;
      q.add(s);
      while (!q.isEmpty()) {
        int x = (int)q.removeFirst();
        for (int i = 0; i < g[x].size(); i++) {
          Edge e = (Edge)edges.get((int)g[x].get(i));
          if (a[e.to] == 0 && e.cap > e.flow) {
            p[e.to] = (int)g[x].get(i);
            a[e.to] = Math.min(a[x], e.cap - e.flow);
            q.add(e.to);
          }
        }
        if (a[t] > 0) break;
      }
      if (a[t] == 0) break;
      for (int u = t; u != s; u = ((Edge)edges.get(p[u])).from) {
        ((Edge)edges.get(p[u])).flow += a[t];
        ((Edge)edges.get(p[u]^1)).flow -= a[t];
      }
      flow += a[t];
    }
    return flow;
  }
}
public class Main {
  static final int INF = Integer.MAX_VALUE / 2;
  static Scanner cin = new Scanner(System.in);
  static void solve() {
    int n = cin.nextInt();
    int m = cin.nextInt();
    EdmondsKarp edmondsKarp = new EdmondsKarp(n);
    for (int i = 0; i < m; i++) {
      int x = cin.nextInt() - 1;
      int y = cin.nextInt() - 1;
      int c = cin.nextInt();
      edmondsKarp.addEdge(x, y, c);
    }
    System.out.println(edmondsKarp.getMaxFlow(0, n - 1));
  }
  public static void main(String[] args) {
    int t = cin.nextInt();
    for (int i = 1; i <= t; i++) {
      System.out.printf("Case %d: ", i);
      solve();
    }
  }
}

你可能感兴趣的:(HDU 3549 Flow Problem)