-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.cpp
More file actions
140 lines (108 loc) · 2.27 KB
/
temp.cpp
File metadata and controls
140 lines (108 loc) · 2.27 KB
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
const int inf = 111111111;
bool mark[111];
queue <int> q;
vector <int> vec[111];
int n, m, s, t, arr[111][111], weight[111][111], par[111];
bool bfs()
{
memset(par, 0, sizeof(par));
memset(mark, false, sizeof(mark));
while(!q.empty())
q.pop();
q.push(s), mark[s] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = 0; i < vec[u].size(); ++i)
{
if (arr[u][vec[u][i]] > 0 && !mark[vec[u][i]])
{
par[vec[u][i]] = u;
q.push(vec[u][i]);
mark[vec[u][i]] = true;
}
}
}
return mark[t];
}
int capacity()
{
int u = t, mn = inf;
while (u != s)
mn = min(mn, arr[par[u]][u]), u = par[u];
u = t;
while (u != s)
{
arr[par[u]][u] -= mn;
arr[u][par[u]] += mn;
u = par[u];
}
return mn;
}
int solve()
{
int ans = 0;
while (bfs())
ans += capacity();
return ans;
}
void dfs(int u)
{
mark[u] = true;
for (int i = 0; i < vec[u].size(); ++i)
{
if (arr[i][vec[u][i]] > 0 && !mark[vec[u][i]])
dfs(vec[u][i]);
}
}
int main()
{
memset(arr, 0, sizeof(arr));
cin >> n >> m >> s >> t;
int u, v, w;
for (int i = 0; i < m; ++i)
{
cin >> u >> v >> w;
weight[u][v] = arr[u][v] = w;
vec[u].push_back(v);
vec[v].push_back(u);
}
cout << "Max Flow : " << solve() << endl;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (weight[i][j])
cout << i << "->" << j << " = " << weight[i][j] - arr[i][j] << endl;
}
}
memset(mark, false, sizeof(mark));
dfs(s);
cout << "Min-Cut Edges :" << endl;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (weight[i][j] && mark[i] && !mark[j])
cout << i << " " << j << endl;
}
}
return 0;
}
/*
6 10 1 6
1 2 16
1 3 13
2 3 10
2 4 12
3 2 4
3 5 14
4 3 9
4 6 20
5 4 7
5 6 4
*/