Pagini recente » Cod sursa (job #1730133) | Cod sursa (job #1326086) | Cod sursa (job #104358) | Cod sursa (job #2617513) | Cod sursa (job #431400)
Cod sursa(job #431400)
#include<fstream>
#include<vector>
#include<queue>
#define nmax 1010
using namespace std;
vector <int> v[nmax];
int padre[nmax],f[nmax][nmax];
int n;
int bfs()
{
int i,nod;
vector <int> :: iterator fiu;
for(i=1;i<=n;i++)
padre[i]=0;
queue <int> Q;
Q.push(1);
padre[1]=1;
while(!Q.empty())
{
nod=Q.front();
Q.pop();
if(nod!=n)
for(fiu=v[nod].begin();fiu!=v[nod].end();fiu++)
if(f[nod][*fiu]&&!padre[*fiu])
{
Q.push(*fiu);
padre[*fiu]=nod;
}
}
return padre[n];
}
int main()
{
vector <int> :: iterator fiu;
int m,x,y,cap,nod,add,flow=0;
ifstream read("maxflow.in");
ofstream write("maxflow.out");
read>>n>>m;
while(m--)
{
read>>x>>y>>cap;
v[x].push_back(y);
v[y].push_back(x);
f[x][y]=cap;
}
while(bfs())
for(fiu=v[n].begin();fiu!=v[n].end();fiu++)
if(padre[*fiu]&&f[*fiu][n])
{
add=(1<<30);
padre[n]=*fiu;
nod=n;
while(nod!=1)
{
if(add>f[padre[nod]][nod])
add=f[padre[nod]][nod];
nod=padre[nod];
}
nod=n;
while(nod!=1)
{
f[padre[nod]][nod]-=add;
f[nod][padre[nod]]+=add;
nod=padre[nod];
}
flow+=add;
}
write<<flow;
return 0;
}