Pagini recente » Cod sursa (job #2380153) | Cod sursa (job #2740731) | Cod sursa (job #1819734) | Cod sursa (job #2949030) | Cod sursa (job #1120130)
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector <int> a[1000];
queue <int> q;
int v[1005][1005],n,m,viz[1005],pred[1005],urm[1005];
int nod,i,x,y,c,fmax=0,n1,d1;
int bfs()
{
memset(viz,0,sizeof(viz));
memset(pred,0,sizeof(pred));
q.push(1);
viz[1]=1;
while(!q.empty())
{
nod=q.front();
q.pop();
for(vector<int>::iterator it=a[nod].begin();it!=a[nod].end();it++)
{
n1=(*it);
if(!viz[n1]&&v[nod][n1])
{
viz[n1]++;
q.push(n1);
pred[n1]=nod;
if(n1==n)
{
while(!q.empty())
q.pop();
break;
}
}
}
}
nod=n;
y=110010;
while(pred[nod])
{
if(v[pred[nod]][nod]< y)
y=v[pred[nod]][nod];
nod=pred[nod];
}
if(y==110010)
return 0;
else
{
nod=n;
while(pred[nod])
{
v[pred[nod]][nod]-=y;
v[nod][pred[nod]]+=y;
nod=pred[nod];
}
return y;
}
}
int main()
{
//freopen("maxflow.in","r",stdin);
//scanf("%d",&n);
//scanf("%d",&m);
f>>n>>m;
for(i=1;i<=m;i++)
{
//scanf("%d",&x);
//scanf("%d",&y);
//scanf("%d",&c);
f>>x>>y>>c;
a[x].push_back(y);
a[y].push_back(x);
v[x][y]=c;
}
x=bfs();
while(x)
{
fmax+=x;
x=bfs();
}
g<<fmax<<'\n';
return 0;
}