Pagini recente » Cod sursa (job #928191) | Cod sursa (job #2746284) | Cod sursa (job #1469547) | Cod sursa (job #2125106) | Cod sursa (job #1963806)
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
#define nmax 1024
#define inf INT_MAX
int n, m, c[nmax][nmax], tt[nmax], f[nmax][nmax];
vector<int> g[nmax];
void citire()
{
int i, x, y, z;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>z;
g[x].push_back(y);
g[y].push_back(x);
c[x][y]+=z;
}
fin.close();
}
int bfs()
{
queue<int> q;
vector<int>::iterator it;
bool viz[nmax]={0};
int k;
viz[1]=1; q.push(1);
while(!q.empty())
{
k=q.front();
q.pop();
for(it=g[k].begin(); it!=g[k].end(); it++)
if(!viz[*it] && c[k][*it]!=f[k][*it])
{
viz[*it]=1;
tt[*it]=k;
q.push(*it);
if(*it==n)
return 1;
}
}
return 0;
}
int main()
{
int flow, fmin, nod;
citire();
flow=0;
while(bfs())
{
fmin=inf;
for(nod=n; nod!=1; nod=tt[nod])
fmin=min(fmin, c[tt[nod]][nod]-f[tt[nod]][nod]);
for(nod=n; nod!=1; nod=tt[nod])
{
f[tt[nod]][nod]+=fmin;
f[nod][tt[nod]]-=fmin;
}
flow+=fmin;
}
fout<<flow<<'\n';
fout.close();
return 0;
}