include iostream include conio include memory include stdio include ti

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#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;
}
}