Pagini recente » Cod sursa (job #1951671) | Cod sursa (job #1661029) | Cod sursa (job #2191888) | Cod sursa (job #1575818) | Cod sursa (job #411693)
Cod sursa(job #411693)
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define pb push_back
#define DIM 1005
int f[DIM][DIM],c[DIM][DIM];
vector <int> g[DIM];
queue <int> q;
int n,m,flow;
int t[DIM];
void read ()
{
int i,x,y,z;
scanf ("%d%d",&n,&m);
for (i=1; i<=m; ++i)
{
scanf ("%d%d%d",&x,&y,&z);
g[x].pb (y);
g[y].pb (x);
c[x][y]=z;
}
}
inline int bf ()
{
vector <int> :: iterator it;
int nod;
memset (t,0,sizeof (t));
t[1]=-1;
for (q.push (1); !q.empty (); q.pop ())
{
nod=q.front ();
for (it=g[nod].begin (); it!=g[nod].end (); ++it)
if (!t[*it] && c[nod][*it]>f[nod][*it])
{
t[*it]=nod;
q.push (*it);
}
}
return t[n];
}
void solve ()
{
int nrmin,i,j;
for ( ; bf (); )
for (i=1; i<=n; ++i)
if (t[i] && c[i][n]>f[i][n])
{
nrmin=c[i][n]-f[i][n];
for (j=i; j!=1; j=t[j])
nrmin=min (nrmin,c[t[j]][j]-f[t[j]][j]);
f[i][n]+=nrmin;
f[n][i]-=nrmin;
for (j=i; j!=1; j=t[j])
{
f[t[j]][j]+=nrmin;
f[j][t[j]]-=nrmin;
}
flow+=nrmin;
}
printf ("%d",flow);
}
int main ()
{
freopen ("maxflow.in","r",stdin);
freopen ("maxflow.out","w",stdout);
read ();
solve ();
return 0;
}