ford-fulkerson--implementation (save first)

8 篇文章 0 订阅



Java代码 
  1.   1import java.util.LinkedList;    
  2.   2import java.util.Queue;    
  3.   3import java.util.Scanner;    
  4.   4.     
  5.   5public class MaxFlow    
  6.   6. {    
  7.   7.     
  8.   8.     private int capacity[][];    
  9.   9.     private int flow[][];    
  10.  10.     private boolean visited[];    
  11.  11.     private int pre[];    
  12.  12.     private int nodes;    
  13.  13.     
  14.  14.     public MaxFlow( int[][] capacity, int nodes )    
  15.  15.     {    
  16.  16.         this.capacity = capacity;    
  17.  17.         this.nodes = nodes;    
  18.  18.         this.flow = new int[nodes][nodes];    
  19.  19.         this.pre = new int[nodes];    
  20.  20.         this.visited = new boolean[nodes];    
  21.  21.     }    
  22.  22.     
  23.  23.     public int maxFlow( int src, int des )    
  24.  24.     {    
  25.  25.         int maxFlow = 0;    
  26.  26.             
  27.  27.         forint i = 0; i < nodes; i++ )    
  28.  28.             forint j = 0; j < nodes; j++ )    
  29.  29.                 flow[i][j] = 0;    
  30.  30.     
  31.  31.         whiletrue )//find a augment path    
  32.  32.         {    
  33.  33.             forint i = 0; i < nodes; i++ )    
  34.  34.             {    
  35.  35.                 visited[i] = false;    
  36.  36.             }    
  37.  37.             pre[src] = -1;    
  38.  38.                 
  39.  39.             if(!BFS( src, des )){// the BFS     
  40.  40.                 break;    
  41.  41.             }    
  42.  42.                 
  43.  43.             /*DFS(src,des);//DFS  
  44.  44.             if(!visited[des])  
  45.  45.                 break;*/    
  46.  46.                 
  47.  47.             int increment = Integer.MAX_VALUE;    
  48.  48.             forint i = des; pre[i] >= src; i = pre[i] )    
  49.  49.             {    
  50.  50.                 //find the min flow of the path    
  51.  51.                 increment = Math.min( increment, capacity[pre[i]][i]    
  52.  52.                         - flow[pre[i]][i] );    
  53.  53.             }    
  54.  54.                 
  55.  55.             //update the flow    
  56.  56.             forint i = des; pre[i] >= src; i = pre[i] )    
  57.  57.             {    
  58.  58.                 flow[pre[i]][i] += increment;    
  59.  59.                 flow[i][pre[i]] -= increment;    
  60.  60.             }    
  61.  61.             //increase the maxFow with the increment     
  62.  62.             maxFlow += increment;    
  63.  63.         }    
  64.  64.         return maxFlow;    
  65.  65.     }    
  66.  66.     
  67.  67.     private void DFS(int src, int des){    
  68.  68.         visited[src] = true;    
  69.  69.         for(int i = 0; i < nodes; i++){    
  70.  70.             if(!visited[i] && ( capacity[src][i] - flow[src][i] > 0) ){    
  71.  71.                 pre[i] = src;//record the augment path    
  72.  72.                 visited[i] = true;    
  73.  73.                 DFS(i,des);    
  74.  74.             }    
  75.  75.         }    
  76.  76.     }    
  77.  77.         
  78.  78.     private boolean BFS( int src, int des )    
  79.  79.     {    
  80.  80.         Queue<Integer> queue = new LinkedList<Integer>();    
  81.  81.         queue.add( src );    
  82.  82.         visited[src] = true;    
  83.  83.         while( !queue.isEmpty() )    
  84.  84.         {    
  85.  85.             int node = queue.poll();    
  86.  86.             forint i = 0; i < nodes; i++ )    
  87.  87.             {    
  88.  88.                 if( !visited[i] && (capacity[node][i] - flow[node][i] > 0) )    
  89.  89.                 {    
  90.  90.                     queue.add( i );    
  91.  91.                     visited[i] = true;    
  92.  92.                     pre[i] = node;//record the augment path    
  93.  93.                 }    
  94.  94.             }    
  95.  95.         }    
  96.  96.     
  97.  97.         return visited[des];    
  98.  98.     }    
  99.  99.     
  100. 100.     public static void main( String[] args )    
  101. 101.     {    
  102. 102.     
  103. 103.         int nodes, edges;    
  104. 104.         Scanner scanner = new Scanner( System.in );    
  105. 105.             
  106. 106.         nodes = scanner.nextInt();    
  107. 107.         edges = scanner.nextInt();    
  108. 108.     
  109. 109.         int[][] capacity = new int[nodes][nodes];    
  110. 110.     
  111. 111.         int src, des, c;    
  112. 112.         forint i = 0; i < edges; i++ )    
  113. 113.         {    
  114. 114.             src = scanner.nextInt();    
  115. 115.             des = scanner.nextInt();    
  116. 116.             c = scanner.nextInt();    
  117. 117.             capacity[src][des] = c;    
  118. 118.         }    
  119. 119.     
  120. 120.         MaxFlow maxFlow = new MaxFlow( capacity, nodes );    
  121. 121.         System.out.println( maxFlow.maxFlow( 0, nodes - 1 ) );    
  122. 122.     }    
  123. 123. }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值