Pagini recente » Cod sursa (job #28136) | Cod sursa (job #1071794) | Cod sursa (job #818883) | Cod sursa (job #2432099) | Cod sursa (job #1553538)
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
#include<fstream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector<int> G[1001];
int N, M, a, b, c, i, T = 0;
int viz[1001];
int from[1001], O[1001], A[1001][1001];
int BFS(int x)
{
queue<int> q;
q.push(x);
viz[x] = T + 1;
int i;
while (q.size())
{
x = q.front();
q.pop();
for (i = 0; i < G[x].size();++i)
if (viz[G[x][i]] <= T && A[x][G[x][i]]>0)
{
q.push(G[x][i]);
viz[G[x][i]] = T + 1;
from[G[x][i]] = x;
if (G[x][i] == N)
{
return 1;
}
}
}
return 0;
}
void update(int x)
{
int min_value = 1 << 30;
int root = x;
if (A[from[x]][x]==0)
return;
while (from[x])
{
min_value = min(min_value, A[from[x]][x]);
x = from[x];
}
if (min_value == 0)
return;
x = root;
while (from[x])
{
if (A[from[x]][x] - min_value >= 0)
{
A[from[x]][x] = A[from[x]][x] - min_value;
A[x][from[x]] = A[x][from[x]] + min_value;
}
else
{
A[from[x]][x] = A[from[x]][x] + min_value;
A[x][from[x]] = A[x][from[x]] - min_value;
}
x = from[x];
}
}
void Edmond()
{
while (BFS(1))
{
update(N);
++T;
}
}
int main()
{
in >> N >> M;
for (i = 1; i <= M; ++i)
{
in >> a >> b >> c;
G[a].push_back(b);
A[a][b] = c;
G[b].push_back(a);
}
for (i = 0; i < G[1].size(); ++i)
O[i] = A[1][G[1][i]];
Edmond();
int s = 0;
for (i = 0; i < G[1].size(); ++i)
s += O[i] - A[1][G[1][i]];
out << s;
return 0;
}