Pagini recente » Cod sursa (job #110239) | Cod sursa (job #1382174) | Cod sursa (job #1710560) | Cod sursa (job #19787) | Cod sursa (job #2720645)
#include <bits/stdc++.h>
#define oo 0x3f3f3f3f
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m, x, y, z, C[1005][1005], F[1005][1005], flow, t[1005];
bool viz[1005];
vector<int> graf[1005];
void Read()
{
f>>n>>m;
for(int i = 1;i <= m;++i)
f>>x>>y>>z, C[x][y] = z, graf[x].push_back(y), graf[y].push_back(x);
}
bool bfs()
{
queue<int> q;
q.push(1);
memset(viz, false, sizeof(viz));
viz[1] = true;
while(!q.empty())
{
int nod = q.front();
q.pop();
if(nod == n) continue;
for(auto it : graf[nod])
{
if(C[nod][it] == F[nod][it] || viz[it]) continue;
viz[it] = true;
q.push(it);
t[it] = nod;
}
}
return viz[n];
}
void Solve()
{
for(;bfs();)
{
for(auto it : graf[n])
{
int nod = it;
if(C[nod][n] == F[nod][n] || !viz[nod]) continue;
int fmin = oo;
t[n] = nod;
for(nod = n;nod != 1;nod = t[nod])
fmin = min(fmin, C[t[nod]][nod] - F[t[nod]][nod]);
if(!fmin) continue;
for(nod = n; nod != 1;nod = t[nod])
F[t[nod]][nod] += fmin, F[nod][t[nod]] -= fmin;
flow += fmin;
}
}
g<<flow;
}
int main()
{
Read();
Solve();
return 0;
}