-
Notifications
You must be signed in to change notification settings - Fork 2
/
Max Flow Min Cut.cpp
154 lines (152 loc) · 4.07 KB
/
Max Flow Min Cut.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
#include<bits/stdc++.h>
#define e 0.000001
using namespace std;
int grid[201][201], temp_grid[201][201];
vector<int> links[201];
long double dist(complex<long long> temp)
{
return (long double)((temp.real() * temp.real()) + (temp.imag() * temp.imag()));
}
bool bfs(int grid[201][201], int source, int destination, int parent[], int n)
{
bool visited[n + 1], flag = true;
for(int i = 0; i <= n; i++)
visited[i] = false;
queue <int> q;
q.push(source);
visited[source] = true;
parent[source] = -1;
while (!q.empty() && flag)
{
int u = q.front();
q.pop();
for (auto v : links[u])
{
if (!visited[v] && grid[u][v] > 0)
{
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
if(visited[destination])
flag = false;
}
return visited[destination];
}
int fordFulkerson(int grid[201][201], int source, int destination, int n)
{
int parent[n + 1];
int max_flow = 0;
while(bfs(grid, source, destination, parent, n))
{
int u, v;
int path_flow = INT_MAX;
for(v = destination; v != source; v = parent[v])
{
u = parent[v];
path_flow = min(path_flow, grid[u][v]);
}
for(v = destination; v != source; v = parent[v])
{
u = parent[v];
grid[u][v] -= path_flow;
grid[v][u] += path_flow;
}
max_flow += path_flow;
}
return max_flow;
}
int main()
{
int n, i, j, destination = 0, flag, max_monkeys = 0, flag1, flag2 = 0;
long long x, y;
long double c;
bool possible = false;
cin>>n>>c;
c *= c;
int monkeys[n], capacity[n];
complex<long long> coordinates[n];
for(i = 0; i < n; i++)
{
cin>>x>>y>>monkeys[i]>>capacity[i];
max_monkeys += monkeys[i];
coordinates[i] = {x, y};
if(monkeys[i] > capacity[i])
{
flag2++;
destination = i;
}
}
if(flag2 > 1)
{
cout<<"-1";
return 0;
}
for(i = 0; i < n; i++)
{
flag = flag1 = 0;
for(j = i + 1; j < n; j++)
{
flag1 = 1;
if(dist(coordinates[i] - coordinates[j]) <= c || abs(dist(coordinates[i] - coordinates[j]) - c) <= e)
{
flag = 1;
links[i].push_back(j);
links[j].push_back(i);
grid[i][j] = capacity[i];
grid[j][i] = capacity[j];
}
}
if(!flag && flag1 && monkeys[i])
{
cout<<"-1";
return 0;
}
links[n].push_back(i);
links[i].push_back(n);
grid[n][i] = monkeys[i];
grid[i][n] = grid[i][i] = 0;
}
if(flag2 == 1)
{
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
temp_grid[i][j] = grid[i][j];
if(max_monkeys == fordFulkerson(temp_grid, n, destination, n))
{
cout<<destination<<"\n";
for(i = 0; i <= n; i++)
{
for(j = 0; j <= n; j++)
cout<<temp_grid[i][j]<<" ";
cout<<"\n";
}
}
else
cout<<"-1";
}
else
{
for(destination = 0; destination < n; destination++)
{
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
temp_grid[i][j] = grid[i][j];
if(max_monkeys == fordFulkerson(temp_grid, n, destination, n))
{
possible = true;
cout<<destination<<"\n";
for(i = 0; i <= n; i++)
{
for(j = 0; j <= n; j++)
cout<<temp_grid[i][j]<<" ";
cout<<"\n";
}
}
}
if(!possible)
cout<<"-1";
}
return 0;
}