Pagini recente » Cod sursa (job #119328) | Cod sursa (job #116392) | Cod sursa (job #1643915) | Borderou de evaluare (job #133032) | Cod sursa (job #719222)
Cod sursa(job #719222)
#include <cstdio>
#define N 1005
#define inf 1999999999
using namespace std;
struct nod{ int val; nod *urm; }*G[N],*GT[N];
int n,m,maxflow;
int coada[N],viz[N],pas,c[N][N],flux[N][N],tata[N];
void read()
{ nod *aux;
int i,x,y,z;
freopen("maxflow.in","r",stdin); scanf("%d %d\n",&n,&m);
for(i=1;i<=m;++i)
{
scanf("%d %d %d\n",&x,&y,&z);
c[x][y]=z;
aux=new nod; aux->val=y; aux->urm=G[x]; G[x]=aux;
aux=new nod; aux->val=x; aux->urm=GT[y]; GT[y]=aux;
}
fclose(stdin);
}
inline int min(int x,int y)
{
if(x<y)return x; else return y; return 0;
}
int bf()
{ int p,u;
nod *aux;
p=u=1; coada[1]=1; viz[1]=pas;
while(p<=u)
{
if(coada[p]!=n)
{
for(aux=G[coada[p]];aux!=NULL;aux=aux->urm)
if(viz[aux->val]!=pas&&c[coada[p]][aux->val]>flux[coada[p]][aux->val])
{
coada[++u]=aux->val; tata[aux->val]=coada[p]; viz[aux->val]=pas;
}
}
++p;
}
if(viz[n]==pas)return 1;
return 0;
}
void solve()
{ int i,fm;
nod *aux;
pas=1;
while(bf())
{
for(aux=GT[n];aux!=NULL;aux=aux->urm)
if(c[aux->val][n]>flux[aux->val][n]&&viz[aux->val]==pas)
{
tata[n]=aux->val; fm=inf;
for(i=n;i!=1;i=tata[i])
fm=min(fm,c[tata[i]][i]-flux[tata[i]][i]);
maxflow+=fm;
for(i=n;i!=1;i=tata[i])
flux[tata[i]][i]+=fm;
}
++pas;
}
}
int main()
{
read();
maxflow=0;
solve();
freopen("maxflow.out","w",stdout); printf("%d",maxflow); fclose(stdout);
return 0;
}