Pagini recente » Cod sursa (job #2150813) | Cod sursa (job #1201121) | Cod sursa (job #3155201) | Cod sursa (job #3265356) | Cod sursa (job #2460502)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int INF = 1e9;
const int DIM = 1007;
vector <int> v[DIM];
int cap[DIM][DIM];
int flow;
int from[DIM];
bitset <DIM> vis;
int n;
queue <int> q;
bool bfs()
{
vis.reset();
vis[1] = true;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto i : v[nod])
if(vis[i] == false && cap[nod][i] != 0)
{
vis[i] = true;
if(i != n)
q.push(i);
from[i] = nod;
}
}
if(vis[n] == true)
return true;
else
return false;
}
main()
{
int m;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
cap[x][y] += c;
v[x].push_back(y);
v[y].push_back(x);
}
while(bfs())
for(auto i : v[n])
if(cap[i][n] && vis[i] == true)
{
int low = INF;
from[n] = i;
for(int p = n; p != 1; p = from[p])
low = min(low, cap[from[p]][p]);
flow += low;
for(int p = n; p != 1; p = from[p])
{
cap[from[p]][p] -= low;
cap[p][from[p]] += low;
}
}
fout << flow;
}