-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
161 lines (141 loc) · 4.2 KB
/
main.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Sergeev Artemiy, 33601/2
* Dinic's algorithm
*/
#include <stdio.h>
#include "graph.h"
/* Debug memory check support */
#ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC
# include <crtdbg.h>
# define SetDbgMemHooks() \
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF | \
_CRTDBG_ALLOC_MEM_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)); \
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); \
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); \
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
#else
#define SetDbgMemHooks() 0
#endif /* _DEBUG */
#define BEGIN_STR "***********************************\n"
#define END_STR "***********************************\n\n"
void printMenu( void )
{
printf("0 - Exit\n"
"1 - Create and use network\n"
"2 - Add edge to network\n"
"3 - Set edge capacity\n"
"4 - Delete edge from network\n"
"5 - Get network maximum flow\n"
"Your choice: ");
}
int main( void )
{
/* Memory leaks handle function */
SetDbgMemHooks();
bool canRun = true;
unsigned int choice = 0;
flow::network_t *currentNetwork = 0;
while (canRun)
{
printf(BEGIN_STR);
printMenu();
scanf("%u%*c", &choice);
switch (choice)
{
case 0:
canRun = false;
break;
case 1:
unsigned int verticesCount, source, sink;
char isSure;
printf("This action will clear current network. Are you sure? (Y/N): ");
scanf("%c", &isSure);
if (isSure == 'Y')
{
printf("Network vertices count is: ");
scanf("%u", &verticesCount);
printf("Network vertices source-vertex number is: ");
scanf("%u", &source);
printf("Network vertices sink-vertex number is: ");
scanf("%u", &sink);
if (currentNetwork)
delete currentNetwork;
currentNetwork = new flow::network_t(verticesCount, source, sink);
}
break;
case 2:
if (currentNetwork)
{
unsigned int from, to, capacity;
int oldCapacity;
printf("Edge begin-vertex number will be: ");
scanf("%u", &from);
printf("Edge begin-vertex number will be: ");
scanf("%u", &to);
printf("Edge capacity will be: ");
scanf("%u", &capacity);
oldCapacity = currentNetwork->addEdge(from, to, capacity);
if (oldCapacity < 0)
printf("Some error was occured\n");
else
printf("Edge was created successfully. Old edge capacity was: %i\n", oldCapacity);
}
else
printf("Try to create network, at first!\n");
break;
case 3:
if (currentNetwork)
{
unsigned int from, to, capacity;
int oldCapacity;
printf("Edge begin-vertex number is: ");
scanf("%u", &from);
printf("Edge begin-vertex number is: ");
scanf("%u", &to);
printf("New edge capacity will be: ");
scanf("%u", &capacity);
oldCapacity = currentNetwork->setEdgeCapacity(from, to, capacity);
if (capacity < 0)
printf("Some error was occured\n");
else
printf("New edge capacity was set. Old edge capacity was: %i\n", oldCapacity);
}
else
printf("Try to create network, at first!\n");
break;
case 4:
if (currentNetwork)
{
unsigned int from, to;
int capacity;
printf("Edge begin-vertex number is: ");
scanf("%u", &from);
printf("Edge begin-vertex number is: ");
scanf("%u", &to);
capacity = currentNetwork->deleteEdge(from, to);
if (capacity < 0)
printf("Some error was occured\n");
else
printf("Edge was deleted successfully. Old edge capacity was: %i\n", capacity);
}
else
printf("Try to create network, at first!\n");
break;
case 5:
if (currentNetwork)
{
int flow;
flow = currentNetwork->maximumFlow();
printf("Maximum network flow is: %i\n", flow);
}
else
printf("Try to create network, at first!\n");
break;
}
printf(END_STR);
}
if (currentNetwork)
delete currentNetwork;
return 0;
}