Pagini recente » Cod sursa (job #3140754) | Cod sursa (job #2026806) | Statistici barbu antonia elena (barbuantonia) | Profil Catalina13 | Cod sursa (job #1385007)
#include <fstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <queue>
#define mp make_pair
#define pb push_back
#define INF numeric_limits<int>::max()
#define int64 long long
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector< vector<int> > a;
queue<int> q;
int flow[1005][1005],n,k,s,d,maxflow,pre[1005],f[1005];
//bfs
void bfs(int start)
{
for(int i=1;i<=n;i++)pre[i]=0;
k=0;
pre[s]=s;
q.push(start);
while(!q.empty())
{
int x=q.front();q.pop();
for(vector<int>::iterator i=a[x].begin();i!=a[x].end();i++)
if(pre[*i]==0 && flow[x][*i]>0)
{
if(*i==d)
{
f[++k]=x;
continue;
}
pre[*i]=x;
q.push(*i);
}
}
}
int main()
{
int m;
in>>n>>m;
a=vector< vector<int> > (n+1);
for(;m;m--)
{
int x,y,z;
in>>x>>y>>z;
a[x].pb(y);
a[y].pb(x);
flow[x][y]=z;
}
for(s=1,d=n;;)//sursa, destinatie
{
bfs(s);
if(k==0)break;
for(int i=1;i<=k;i++)
{
int mx=INF;
pre[d]=f[i];
for(int x=d;pre[x]!=x;x=pre[x])
mx=min(mx,flow[pre[x]][x]);
for(int x=d;pre[x]!=x;x=pre[x])
{
flow[pre[x]][x]-=mx;
flow[x][pre[x]]+=mx;
}
maxflow+=mx;
}
}
out<<maxflow;
return 0;
}