网络流之增广路算法

最大流算法-- 基于增广路的实现(java版)

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <queue>  
  4. using namespace std;  
  5.   
  6. //Suppose there are only 10 vertexes at most  
  7. const int MAX_VERTEX = 10;  
  8. const int MAX_VALUE = 1<<30;  
  9.   
  10. int graph[MAX_VERTEX][MAX_VERTEX];  
  11.   
  12. int vertex_number;  
  13. queue<int> fifo_queue;  
  14. //store current vertex's prefix vertex  
  15. int prefix[MAX_VERTEX];  
  16. //store stream size of path which start s and end with current node  
  17. int stream[MAX_VERTEX];  
  18.   
  19. /** 
  20.  * v0-s 
  21.  * v1-v1 
  22.  * v2-v2 
  23.  * v3-t 
  24.  */  
  25. void BuildGraph() {  
  26.     vertex_number=6;  
  27.     for (int i=0; i<MAX_VERTEX; i++)  
  28.         memset(graph[i], -1, sizeof(graph[i]));  
  29.     //Forward stream define  
  30.     graph[0][1] = 16;  
  31.     graph[0][2] = 13;  
  32.     graph[1][3] = 12;  
  33.     graph[2][3] = 0;  
  34.     graph[1][2] = 10;  
  35.     graph[2][4] = 14;  
  36.     graph[4][3] = 7;  
  37.     graph[4][5] = 4;  
  38.     graph[3][5] = 20;  
  39.     //Reverse stream define  
  40.     graph[1][0] = 0;  
  41.     graph[2][0] = 0;  
  42.     graph[3][1] = 0;  
  43.     graph[3][2] = 9;  
  44.     graph[2][1] = 4;  
  45.     graph[4][2] = 0;  
  46.     graph[3][4] = 0;  
  47.     graph[5][4] = 0;  
  48.     graph[5][3] = 0;  
  49. }  
  50.   
  51. void ClearQueue() {  
  52.     while (!fifo_queue.empty())  
  53.         fifo_queue.pop();  
  54. }  
  55.   
  56. int Min(int a, int b) {  
  57.     return a < b ? a : b;  
  58. }  
  59.   
  60. void SearchTarget(int start, int stop) {  
  61.     if (start == stop)  
  62.         return;  
  63.     int max_stream=0;  
  64.     while (true) {  
  65.         ClearQueue();  
  66.         memset(prefix, -1, sizeof(prefix));  
  67.         memset(stream, 0, sizeof(stream));  
  68.         stream[start] = MAX_VALUE;  
  69.   
  70.         //start BFS  
  71.         fifo_queue.push(start);  
  72.         while (!fifo_queue.empty()) {  
  73.             int current = fifo_queue.front();fifo_queue.pop();  
  74.             for (int i=0; i<vertex_number; i++)  
  75.                 if (i != current && graph[current][i] > 0 && prefix[i]==-1) {  
  76.                     fifo_queue.push(i);  
  77.                     prefix[i] = current;  
  78.                     //store current path's stream size  
  79.                     stream[i] = min(stream[current], graph[current][i]);  
  80.                     if (i == stop) {  
  81.                         break;  
  82.                     }  
  83.                 }  
  84.   
  85.             //break if we have found target node  
  86.             if (stream[stop])  
  87.                 break;  
  88.         }  
  89.   
  90.         for (int i=0; i<vertex_number; i++)  
  91.             printf("[path] %d->%d\n", prefix[i], i);  
  92.   
  93.         if (stream[stop]) {  
  94.             int max_value = stream[stop];  
  95.             max_stream += max_value;  
  96.             //modify graph stream value  
  97.             for (int current=stop; current!=start; current = prefix[current]) {  
  98.                 printf("%d->%d, current stream is %d, will use %d \n", prefix[current], current, graph[prefix[current]][current], max_value);  
  99.                 graph[prefix[current]][current] -= max_value;  
  100.                 graph[current][prefix[current]] += max_value;  
  101.             }  
  102.         } else {  
  103.             printf("No available path found, exit now\n");  
  104.             break;  
  105.         }  
  106.     }  
  107.     printf("Max stream size is:%d\n", max_stream);  
  108. }  
  109.   
  110. int main(int argc, char** argv) {  
  111.     BuildGraph();  
  112.     SearchTarget(0, 5);  
  113.     return 0;  
  114. }  

猜你喜欢

转载自blog.csdn.net/gen504240809/article/details/42125077