Advertisement
qwerty787788

Dinnic algorithm O(n^2*m)

Nov 28th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Dinnic {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     class Edge {
  9.         int fr, to;
  10.         int cost;
  11.         int flow;
  12.         Edge backEdge;
  13.  
  14.         public Edge(int fr, int to, int cost) {
  15.             super();
  16.             this.fr = fr;
  17.             this.to = to;
  18.             this.cost = cost;
  19.             flow = 0;
  20.         }
  21.  
  22.     }
  23.      
  24.     ArrayList<Edge>[] g;
  25.     int[] d;
  26.     int[] last;
  27.     int n;
  28.      
  29.     boolean bfs() {
  30.         Arrays.fill(d, -1);
  31.         ArrayList<Integer> queue = new ArrayList<Integer>();
  32.         int it = 0;
  33.         queue.add(0);
  34.         d[0] = 0;
  35.         while (it < queue.size()) {
  36.             int v = queue.get(it);
  37.             for (Edge e : g[v]) {
  38.                 if (d[e.to] == -1 && e.flow < e.cost) {
  39.                     d[e.to] = d[v] + 1;
  40.                     queue.add(e.to);
  41.                 }
  42.             }
  43.             it++;
  44.         }
  45.         return d[n - 1] != -1;
  46.     }
  47.  
  48.     int dfs(int v, int flow) {
  49.         if (flow == 0 || v == n - 1) return flow;
  50.         for (; last[v] < g[v].size(); last[v]++) {
  51.             Edge e = g[v].get(last[v]);
  52.             if (d[e.to] != d[v] + 1) continue;
  53.             int pushed = dfs(e.to, Math.min(flow, e.cost - e.flow));
  54.             if (pushed != 0) {
  55.                 e.flow += pushed;
  56.                 e.backEdge.flow -= pushed;
  57.                 return pushed;
  58.             }
  59.         }
  60.         return 0;
  61.     }
  62.      
  63.     void solve() {
  64.         n = in.nextInt();
  65.         int m = in.nextInt();
  66.         g = new ArrayList[n];
  67.         for (int i = 0; i < n; i++)
  68.             g[i] = new ArrayList<Edge>();
  69.         for (int i = 0; i < m; i++) {
  70.             int fr = in.nextInt() - 1;
  71.             int to = in.nextInt() - 1;
  72.             int cost = in.nextInt();
  73.             Edge e1 = new Edge(fr, to, cost);
  74.             Edge e2 = new Edge(to, fr, 0);
  75.             e1.backEdge = e2;
  76.             e2.backEdge = e1;
  77.             g[fr].add(e1);
  78.             g[to].add(e2);
  79.         }
  80.         d = new int[n];
  81.         last = new int[n];
  82.         int flow = 0;
  83.         while (bfs()) {
  84.             while (true) {
  85.                 Arrays.fill(last, 0);
  86.                 int add = dfs(0, Integer.MAX_VALUE);
  87.                 if (add == 0) break;
  88.                 flow += add;
  89.             }
  90.         }
  91.         out.println(flow);
  92.     }
  93.  
  94.     void run() {
  95.         try {
  96.             in = new FastScanner(new File("maxflow.in"));
  97.             out = new PrintWriter(new File("maxflow.out"));
  98.  
  99.             solve();
  100.  
  101.             out.close();
  102.         } catch (FileNotFoundException e) {
  103.             e.printStackTrace();
  104.         }
  105.     }
  106.  
  107.     void runIO() {
  108.  
  109.         in = new FastScanner(System.in);
  110.         out = new PrintWriter(System.out);
  111.  
  112.         solve();
  113.  
  114.         out.close();
  115.     }
  116.  
  117.     class FastScanner {
  118.         BufferedReader br;
  119.         StringTokenizer st;
  120.  
  121.         public FastScanner(File f) {
  122.             try {
  123.                 br = new BufferedReader(new FileReader(f));
  124.             } catch (FileNotFoundException e) {
  125.                 e.printStackTrace();
  126.             }
  127.         }
  128.  
  129.         public FastScanner(InputStream f) {
  130.             br = new BufferedReader(new InputStreamReader(f));
  131.         }
  132.  
  133.         String next() {
  134.             while (st == null || !st.hasMoreTokens()) {
  135.                 String s = null;
  136.                 try {
  137.                     s = br.readLine();
  138.                 } catch (IOException e) {
  139.                     e.printStackTrace();
  140.                 }
  141.                 if (s == null)
  142.                     return null;
  143.                 st = new StringTokenizer(s);
  144.             }
  145.             return st.nextToken();
  146.         }
  147.  
  148.         boolean hasMoreTokens() {
  149.             while (st == null || !st.hasMoreTokens()) {
  150.                 String s = null;
  151.                 try {
  152.                     s = br.readLine();
  153.                 } catch (IOException e) {
  154.                     e.printStackTrace();
  155.                 }
  156.                 if (s == null)
  157.                     return false;
  158.                 st = new StringTokenizer(s);
  159.             }
  160.             return true;
  161.         }
  162.  
  163.         int nextInt() {
  164.             return Integer.parseInt(next());
  165.         }
  166.  
  167.         long nextLong() {
  168.             return Long.parseLong(next());
  169.         }
  170.     }
  171.  
  172.     public static void main(String[] args) {
  173.         new Dinnic().run();
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement