Pagini recente » Cod sursa (job #240307) | Cod sursa (job #856116) | Cod sursa (job #3173819) | Cod sursa (job #3288011) | Cod sursa (job #1711389)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1009;
class flowClass
{
private :
int f[nmax][nmax] , c[nmax][nmax];
int w[nmax] , p[nmax];
vector < int > g[nmax];
queue < int > iq;
bool path()
{
while (iq.size()) iq.pop();
memset(p , 0 , sizeof(p));
iq.push(source);
p[source] = true;
while (iq.size())
{
int q0 = iq.front();
iq.pop();
if (q0 == sink) continue;
for (int i = 0 ; i < g[q0].size() ; ++i)
{
int nq = g[q0][i];
if (p[nq]) continue;
if (c[q0][nq] > f[q0][nq])
{
w[nq] = q0;
iq.push(nq);
p[nq] = true;
}
}
}
return p[sink];
}
public :
int sink , source;
int flow = 0;
void addEdge(int u , int v , int x)
{
g[u].push_back(v);
g[v].push_back(u);
c[u][v] += x;
}
void work()
{
int q0 , d0 , w0 , t;
while (path())
for (int i = 0 ; i < g[sink].size() ; ++i)
{
d0 = g[sink][i];
if (c[d0][sink] == f[w0][q0]) continue;
if (p[d0] == false) continue;
t = 1 << 30;
w[sink] = d0;
for (q0 = sink ; q0 != source ; q0 = w[q0])
{
w0 = w[q0];
t = min(t , c[w0][q0] - f[w0][q0]);
}
flow += t;
for (q0 = sink ; q0 != source ; q0 = w[q0])
{
w0 = w[q0];
f[w0][q0] += t;
f[q0][w0] -= t;
}
}
}
} solve;
void scan()
{
int m;
solve.source = 1;
scanf("%d" , &solve.sink);
scanf("%d" , &m);
for (int i = 0 ; i < m ; ++i)
{
int u , v , x;
scanf("%d" , &u);
scanf("%d" , &v);
scanf("%d" , &x);
solve.addEdge(u , v , x);
}
}
int main()
{
freopen("maxflow.in" , "r" , stdin);
freopen("maxflow.out" , "w" , stdout);
scan();
solve.work();
cout << solve.flow << '\n';
return 0;
}