#include <iostream>
#include <conio.h>
#include <memory.h>
#include <stdio.h>
#include <time.h>
using namespace std;
const int max_vershin = 40;
int number_vershin;
const int inf = 10000;
int g[max_vershin][max_vershin];
int k[max_vershin][max_vershin];
int fw[max_vershin];
int link[max_vershin];
int course[max_vershin];
int cb, ct;
int FindPath(int head, int end)
{
cb = 0; ct = 1; course[0] = head;
link[end] = -1;
int i;
int course_begin;
memset(fw, 0, sizeof(int)*number_vershin);
fw[head] = inf;
while (link[end] == -1 && cb < ct)
{
course_begin = course[cb];
for (i=0; i<number_vershin; i++)
if ((k[course_begin][i] - g[course_begin][i])>0 && fw[i] == 0)
{
course[ct] = i; ct++;
link[i] = course_begin;
if (k[course_begin][i]-g[course_begin][i] < fw[course_begin])
fw[i] = k[course_begin][i];
else
fw[i] = fw[course_begin];
}
cb++;
}
if (link[end] == -1) return 0;
course_begin = end;
while (course_begin != head)
{
g[link[course_begin]][course_begin] +=fw[end];
course_begin = link[course_begin];
}
return fw[end];
}
int max_fw(int head, int end)
{
memset(g, 0, sizeof(int)*max_vershin*max_vershin);
int max_fw = 0;
int add_fw;
do
{
add_fw = FindPath(head, end);
max_fw += add_fw;
} while (add_fw >0);
return max_fw;
}
int main()
{
setlocale(LC_ALL, "Russian");
int head, end;
cout << "Введите количество вершин:" << endl;
cin >> number_vershin;
int i, j;
srand(time(NULL));
for (i=0; i<number_vershin; i++){
for (j=0; j<number_vershin; j++){
k[i][j] = (rand()%10)%2;
if(k[i][j])
k[i][j] = rand()%10;
k[i][i] = 0;
}
}
for (i=0; i<number_vershin; i++){
for (j=0; j<number_vershin; j++)
cout << k[i][j] << " ";
cout << endl;
}
cout << "Введите начальный элемент:" << endl;
cin >> head;
cout << "Введите конечный элемент:" << endl;
cin >> end;
cout << "Максимальный поток:" << endl;
cout << max_fw(head, end) << endl;
cout << "Матрица потока:" << endl;
cout << " ";
for ( int i = 0; i < number_vershin; i++)
cout << "[" << i << "]" << " ";
cout << endl;
for ( int i = 0; i < number_vershin; i++){
cout << "[" << i << "]" << " ";
for (int j = 0; j < number_vershin; j++)
cout <<g[i][j] << " ";
cout << endl;
}
}