Pagini recente » Cod sursa (job #3130302) | Cod sursa (job #2141674) | Cod sursa (job #525051) | Cod sursa (job #1565137) | Cod sursa (job #453650)
Cod sursa(job #453650)
#include<fstream.h>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
int n,m,cap[1002][1002],flux[1002][1002],sw[1002],sol,t[1002];
vector <int > v[1002];
deque <int> C;
void afisare ()
{
ofstream f("maxflow.out");
f<<sol;
f.close();
}
int getflux ()
{
int i,j,fluxmax=0,max,p;
deque <int> :: iterator it1;
vector <int > :: iterator it;
C.clear();
C.push_back(1);
memset(sw,0,sizeof(sw));
sw[1]=1;
while (C.begin()<C.end())
{
it1=C.begin();
for (it=v[*it1].begin();it<v[*it1].end();++it)
if (sw[*it]==0 && flux[*it1][*it]<cap[*it1][*it] && *it!=n)
{
C.push_back(*it);
sw[*it]=1;
t[*it]=*it1;
}
C.pop_front();
}
for (it=v[n].begin();it<v[n].end();++it)
{
max=cap[*it][n]-flux[*it][n];
for (p=*it;p!=1;p=t[p])
if (max>cap[t[p]][p]-flux[t[p]][p])
max=cap[t[p]][p]-flux[t[p]][p];
fluxmax+=max;
if (max>0)
{
flux[*it][n]+=max;
flux[n][*it]-=max;
for (p=*it;p!=1;p=t[p])
{
flux[t[p]][p]+=max;
flux[p][t[p]]-=max;
}
}
}
return fluxmax;
}
void flow ()
{
int f=1;
while (f>0)
{
f=getflux();
sol+=f;
}
}
void citire ()
{
int i,x,y,z;
ifstream f("maxflow.in");
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y>>z;
cap[x][y]=z;
v[x].push_back(y);
v[y].push_back(x);
}
f.close();
}
int main()
{
citire ();
flow();
afisare ();
return 0;
}